Python Tutorial - Reverse a String Using for loop

Поделиться
HTML-код
  • Опубликовано: 25 авг 2024
  • In this Python Programming video tutorial you will learn how to reverse given string .
    Here in this video tutorial we will learn how to reverse a string using for loop and function in detail with example.
    For more free tutorials on computer programming
    / amulsacademy
    / amulsacademy

Комментарии • 283

  • @papie5151
    @papie5151 4 года назад +17

    I love all your tutorials. I see often in the comments some smart code which for a beginner like myself means nothing- maybe,will be valuable in the future, but your tutorials are in depth which drives in my understanding of the programming concepts. Please keep doing what you are doing. Bless you.

  • @uday2339
    @uday2339 6 лет назад +35

    n =("amuls")
    n1=(n[::-1])
    print(n1)

    • @hemantprasad1018
      @hemantprasad1018 5 лет назад +2

      there are 5 ways to reverse a string in Python

    • @marioshusband3700
      @marioshusband3700 4 года назад +1

      what does the (n[::-1]) mean? I barely learned python.

  • @a42h
    @a42h 6 лет назад +17

    Thank you this was very helpful! I like how you went into details of the for loop and explained the logic of it.

    • @AmulsAcademy
      @AmulsAcademy  6 лет назад +2

      Thank you:)

    • @FI3RZE_PATRIOT
      @FI3RZE_PATRIOT 4 года назад

      @@AmulsAcademy GREAT VIDEO AND WELL EXPLAINED!!!

    • @vidithnarayana2346
      @vidithnarayana2346 4 года назад

      @@AmulsAcademy python code to reverse a string without effecting special characters?(Input: ab,c,d)(output:dc,b,a) please reply

    • @romantrenton3644
      @romantrenton3644 3 года назад

      You all probably dont give a damn but does any of you know of a method to get back into an Instagram account?
      I somehow lost my account password. I appreciate any tricks you can offer me.

    • @hunterjamal6970
      @hunterjamal6970 3 года назад

      @Roman Trenton Instablaster =)

  • @Goshbello-Money-AI-Tutorials
    @Goshbello-Money-AI-Tutorials 5 лет назад +4

    Thank you again for another well explained tutorial. It’s not just the method you used to solve the problem but I have gained a better understanding with FOR LOOP watching this video. You’re a great teacher. I am waiting for your next video. Thank you

  • @suyash7450
    @suyash7450 Год назад

    I have'nt seen anyone explaining concepts so deeply as you do , It makes learning very easy
    thanks mam for your efforts

  • @SB-fs4vi
    @SB-fs4vi 3 года назад +2

    You are really a very good teacher with clear concept. All your videos are helping me a lot as a beginner. Thank you so much🙏🏻

  • @gamingengineer360
    @gamingengineer360 3 года назад +2

    now thats what I call explanation. a k.g. boy can understand the program. thanq mam.

  • @kapishsingh
    @kapishsingh 5 лет назад +9

    def reverse(y):
    for i in range (1,1+len(y)):
    print(y[-i],end='')
    reverse("amulse")

    • @00xess
      @00xess 4 года назад

      why the hell did you pass in 1 at the first place, just use range(len(y)) lmao

    • @panagiotisgoulas8539
      @panagiotisgoulas8539 3 года назад

      @@00xess If he did that he would have to print(y[-i - 1], end='').
      First index is 0 but last is -1

    • @thesumitkumar0
      @thesumitkumar0 3 года назад

      #run_this
      print(input()[-1::-1])

    • @kapishsingh
      @kapishsingh 3 года назад

      @@thesumitkumar0 you can ignore -1 in beginning

  • @ramchandrakulkarni5917
    @ramchandrakulkarni5917 2 года назад +1

    Great Explaination, I was stucked at working of the for loop.
    But now it is clear.
    Thank You.

  • @sambhavsisodia3920
    @sambhavsisodia3920 6 лет назад +43

    l,m,n = yell,yem,yen 😂😂😂 ,whatever video was great !

    • @AmulsAcademy
      @AmulsAcademy  6 лет назад +7

      Thank you:)

    • @MuhammadNaeem-xj8zp
      @MuhammadNaeem-xj8zp 4 года назад

      Yeh it should be el, am ,an for l, m , n any ways great explanation nice voice

    • @rb8934
      @rb8934 4 года назад +3

      the pronunciation differs as per the state

    • @soumyashreebiswal14
      @soumyashreebiswal14 3 года назад +2

      @@MuhammadNaeem-xj8zp Its not about whether it SHOULD BE or whatever. Its totally okay to have a different pronounciation.

    • @chandanshivalingaiah9861
      @chandanshivalingaiah9861 3 года назад

      Nobodys perfect summbbbhaaavvvvvvvaaa ssuuusssoooddiiiaaaaa so please watch it or get out nobody wants you here anyway

  • @lokakshaiathibans484
    @lokakshaiathibans484 3 года назад +5

    Nice and clear explanation.
    How to print this sequence:
    Input : A B C D
    Output : Z Y X W

    • @AmulsAcademy
      @AmulsAcademy  3 года назад +6

      Try this:
      n = input("Enter number of elements : ")
      k=65
      list1 = n.split()
      for i in range(len(list1)):
      c = ord(list1[i])-k
      h = 25-c+k
      print(chr(h))

  • @raheemabdul4410
    @raheemabdul4410 4 года назад +1

    U seem kannadiga from Bangalore..lots of wishes from mangalore

  • @klaskycsupo121
    @klaskycsupo121 4 года назад +1

    So chars are going to be concatenated from right to left? Did I get this right?

  • @balakarna_editz2779
    @balakarna_editz2779 5 лет назад +2

    Nice explanation, please upload python data science videos

  • @shahsikalagowda183
    @shahsikalagowda183 10 месяцев назад

    Too good mam🤍...ur voice next level🙌🏻😇.. understood clearly 😍🥰

  • @smhproductions295
    @smhproductions295 5 месяцев назад

    Feels super easy after seeing this

  • @sayanmukherjee7726
    @sayanmukherjee7726 2 года назад +1

    A great explained video, very useful for me for better understanding of for loop iteration. Thank you.

  • @shwetap3184
    @shwetap3184 10 месяцев назад

    amazing explanation till now superb

  • @davidusharauli2227
    @davidusharauli2227 6 лет назад +10

    How this code reverses the input string? It works but I am not sure if I understood the mechanics of it. Can you explain a little bit more.

    • @AmulsAcademy
      @AmulsAcademy  6 лет назад +30

      def reverse(string):
      reversed_string = ""
      for i in string:
      reversed_string = i + reversed_string
      print("reversed string is:",reversed_string)
      string = input("enter the string:")
      print("entered string is:",string)
      reverse(string)
      Here first ,
      string = input("enter the string") will be executed,
      so you need to enter the input,
      if you enter "amuls" this string will be stored in variable string
      Next it will execute ,
      print("entered string is:",string)
      so it will print entered string to the output screen.
      Next it will execute function call that is,
      reverse (string)
      so now control goes to the reverse function definition,
      so it will execute that function.
      def reverse(string)
      here string is the input that is "amuls",
      next we will take one variable and we assign empty string to that.
      reversed_string = ""
      here reversed_string is a variable which will hold final output.
      next for loop,
      for i in string:
      reversed_string = i + reversed_string
      so here string is "amuls", so initially variable i value will be 'a',
      so it will execute for loop body ,
      reversed_string = i + reversed_string
      here we are doing string concatenation, symbol + is used for concatenation of strings.
      reversed_string is initially empty string and i value is 'a'
      so it will concatenate 'a'and empty string,
      so we will get reversed_string as 'a'
      that is,
      reversed_string = i + reversed_string
      = 'a' + ""
      = 'a'
      next again control goes to for loop,
      now i value becomes 'm',
      it will execute for loop body,
      reversed_string = i + reversed_string
      = 'm' + 'a'
      = "ma"
      in this same way it will continue the execution:)

    • @Amit-nh2pu
      @Amit-nh2pu 6 лет назад

      great!

    • @vengateshprasath2906
      @vengateshprasath2906 5 лет назад +2

      Str="David"
      str[::-1]

    • @melissahuerta8315
      @melissahuerta8315 4 года назад +1

      thanks for the explanation, at first I was confused too.. reading the string as it was an array like str[I] and in my mind it got the same order.. but now is clear :)

    • @sultanahmadsabawoon6030
      @sultanahmadsabawoon6030 4 года назад

      still confused. make the code as reversed_string + i.
      in this case it will give you the same string as entered initially. the trick is that the i value is cancatenated with reversed string not the other way around.

  • @harik1999
    @harik1999 5 лет назад +5

    Hello Ma'am,I have started watching your Python programming videos and the sessions were really helpful as you did explained in a nicely manner.
    Is it possible to navigate all your videos (the concepts + sample program's) please?
    Also,let me know if any materials are available and then I can take as well if they are chargable?

    • @AmulsAcademy
      @AmulsAcademy  5 лет назад

      Sorry I don't have any materials
      :)

  • @moonnyy364
    @moonnyy364 4 года назад +4

    This video helped me so much! Thank you!

  • @Darsky
    @Darsky 4 года назад +3

    Finally! Simple, yet detailed!! Thank you!

  • @kushi5439
    @kushi5439 5 лет назад +2

    thank you very much ma'am ......very easy way of explanation u had given here.....thanks a lot :)
    looking many more python videos from you

  • @mayank_8348
    @mayank_8348 2 года назад

    It was a great explanation ma'am

  • @NewIndianera
    @NewIndianera 6 лет назад

    Mam please make a video on ...Any program how it is executed

  • @riuspablo
    @riuspablo 5 лет назад +21

    Or
    >>> string = "amuls"
    >>> string[::-1]

    • @bagavathi5340
      @bagavathi5340 4 года назад +1

      it's a simple method

    • @vortex_7574
      @vortex_7574 4 года назад +1

      Genius!

    • @vimukthiaravinda6288
      @vimukthiaravinda6288 3 года назад

      this is the right method

    • @jenre6903
      @jenre6903 3 года назад

      you just saved me 8 minutes. thank you, you are a legend

    • @soulfulman4834
      @soulfulman4834 3 года назад

      This is inbuilt method which I use oftenly but it doesn't works in complex programs...

  • @manproactive6649
    @manproactive6649 3 года назад +1

    wonderful! how is crystal clear explanation! very helpful for beginners like for me

  • @mohammedmujtabaahmed8337
    @mohammedmujtabaahmed8337 3 года назад +1

    Excellent video. Thanks@!

  • @puria7711
    @puria7711 Год назад

    thank you! i was having a hard time understanding this concept, i really appreciate it! 😀

  • @mukundmohare310
    @mukundmohare310 2 года назад

    excellent explanation Ma'am keep it up and keep growing.👍👍👍

  • @shreyanshmishra3278
    @shreyanshmishra3278 5 лет назад +3

    Superb Explanation!!!

  • @takerixgaming4170
    @takerixgaming4170 Год назад

    Magnetic voice mam🔥🔥🔥

  • @pavanboggarapu4842
    @pavanboggarapu4842 5 лет назад +1

    way of explanation is too good thank you soo much

  • @paramvirsohi5922
    @paramvirsohi5922 2 года назад

    wow, waht an explanation. I wish I get this fot interview

  • @RazaviImran
    @RazaviImran 5 лет назад +1

    This video is nice, i saw your 4 videos today. But this is great, nice explanation.
    If you able to make video in hindi it will great opportunity for hindi people just like me, i am not good in english

    • @AmulsAcademy
      @AmulsAcademy  5 лет назад

      I will start the Hindi channel soon :)

    • @phaniinakollu4604
      @phaniinakollu4604 4 года назад

      @@AmulsAcademy explain django ecommerce website

  • @anilmacharla6939
    @anilmacharla6939 3 года назад +1

    A million Thanks!

  • @sureshcement2212
    @sureshcement2212 Год назад

    I just love ur videos buddy

  • @abuawaish7
    @abuawaish7 3 года назад +1

    Great explanation mam👍❤️

  • @ralphmachado8201
    @ralphmachado8201 5 лет назад +4

    hi i learn a lot from your videos and check my below code which is more simply than the above one.
    print("enter anything to be reversed")
    x=input()
    length=len(x)
    for i in range(length-1,-1,-1):
    print(x[i],end="")

    • @PassionateSaksham
      @PassionateSaksham 3 года назад

      What the Meaning Of -1,-1,-1 Here Please Answer Me

    • @ralphmachado8201
      @ralphmachado8201 3 года назад +1

      @@PassionateSaksham
      1: length-1 means that the for loop will start from the index number which is one less than the length of the string to be reversed
      2: -1 is the number untill which the for loop will run
      3: -1 is the amount which will be decremented after every iteration and this will continue until the number reaches till -1.

  • @praveentiwari5672
    @praveentiwari5672 5 лет назад +1

    thanks for explaining everything clearly .........thanks a lot........

  • @arfatbagwan48
    @arfatbagwan48 2 года назад

    thank you so much ma'am god bless you !

  • @psiddamma1022
    @psiddamma1022 2 года назад

    Thank you mam for clear explanation

  • @kishoredev6004
    @kishoredev6004 4 года назад +5

    def rev(x):
    return x[::-1]
    rev('forward')

    • @gamerjmax5568
      @gamerjmax5568 3 года назад +1

      x = input("Enter any name: ")
      x[::-1]

  • @Ink_Sack
    @Ink_Sack 4 года назад +2

    Very helpful and easy to understand. Thank you

  • @rajlaxmijoshi8741
    @rajlaxmijoshi8741 2 года назад

    Nice explanation, amazing teacher you are 💓

  • @NewIndianera
    @NewIndianera 6 лет назад

    Mam make video on c++ and html and core and advance java

  • @katsukibakugou6216
    @katsukibakugou6216 3 года назад +1

    thank you so much! please make more videos like this its really helping!

  • @amitabhgupta21
    @amitabhgupta21 5 лет назад +1

    You are very good and your way is best,keep it up

  • @kartheekarun2030
    @kartheekarun2030 Год назад

    thanks for the explanation at the end it was very helpful.

  • @learngurututorials8837
    @learngurututorials8837 4 года назад +1

    Awesome .. explanation.. i really appreciate you.. 👌👌👌

  • @paramvirsohi5922
    @paramvirsohi5922 2 года назад

    Perfect explanation for interview

  • @saikiran307
    @saikiran307 3 года назад +1

    Good Explanation

  • @abhaysrivastav5664
    @abhaysrivastav5664 Год назад

    You constructed the solution in a more complicated way which can be solved with the easy loop system.

  • @shambhushrestha6926
    @shambhushrestha6926 4 года назад +1

    love the way you teach

  • @anuradhakumari6123
    @anuradhakumari6123 4 года назад +1

    Nice explanation

  • @5151sunil
    @5151sunil 6 лет назад +3

    Nice explanation.. Thanks..

  • @dabhijaydeep
    @dabhijaydeep 3 года назад +1

    Can u make decorator using reverse number and also negative number and also string enter to string reverse in single function

  • @emptyFull123
    @emptyFull123 5 лет назад +2

    Thankyou : For detailed explanation 😊

  • @sivapriya3276
    @sivapriya3276 3 года назад +1

    thank you somuch for this wonderful vedio.how to reverse a word using recursion

    • @AmulsAcademy
      @AmulsAcademy  3 года назад

      Thank you :)
      You can use string slicing method and write the program :)

    • @sivapriya3276
      @sivapriya3276 3 года назад

      @@AmulsAcademy can you say the program

  • @estevaofranca5009
    @estevaofranca5009 2 года назад

    Thanks for sharing this!!

  • @muralinaremurali9014
    @muralinaremurali9014 16 дней назад

    love your voice

  • @LM-th7dj
    @LM-th7dj 4 года назад +2

    Thank you so much! That was very helpful!

  • @sujithnair232
    @sujithnair232 4 года назад +1

    Really good explanation, Thank you

  • @SPD360ENGLISH
    @SPD360ENGLISH 2 года назад

    Very good and neat explanation... Well done...

  • @venkatkrishnan876
    @venkatkrishnan876 3 года назад +1

    Well explained mam

  • @sandeepsunny9905
    @sandeepsunny9905 2 года назад

    Nice approach

  • @ASHGaming-yp9vj
    @ASHGaming-yp9vj 2 года назад

    Really great explanation 🙌

  • @rexrajan5232
    @rexrajan5232 9 месяцев назад

    I think there is not necessary to include function def, For loop is enough to reverse the string. So what's your opinion?

  • @shruthi9776
    @shruthi9776 6 лет назад +1

    Fabulous keep going i loved u programmes vry helpful especially during exams😁👍👍👍👏👏👏👏

  • @Tech_Brigadier
    @Tech_Brigadier 2 года назад

    Grate Tutorials, But mam when I was learning python functions I was taught not to use print statements in a function or else the user's rights to edit the returned value will end up

  • @Raja-tt4ll
    @Raja-tt4ll 4 года назад +1

    Thanks a lot

  • @jitendranavagire3380
    @jitendranavagire3380 5 лет назад +1

    thank you so much.. these is very helpful to me .

  • @qasemalharbi38
    @qasemalharbi38 3 года назад +1

    Great 👍 I love it
    please how can we solve it with recursion

    • @AmulsAcademy
      @AmulsAcademy  3 года назад +1

      Try this:
      def reverse_string(s):
      if len(s) == 1:
      return s
      else:
      return reverse_string(s[1:]) + s[0]
      s = input("enter string:")
      print("The original string is : ",s)
      print ("The reversed string is : ",reverse_string(s))

  • @manuchowdary7848
    @manuchowdary7848 4 года назад

    good explanation

  • @NewIndianera
    @NewIndianera 6 лет назад

    Mam make video for those who don't know anything about programing even ABCD of it

  • @sharathcg4697
    @sharathcg4697 2 года назад

    clearly understood
    Thank you.....

  • @tusharkhatri5795
    @tusharkhatri5795 2 года назад

    can function name be any keyword here you have written as reverse which i think is reserved word . Pls explain as code is executing

  • @jitendranavagire3380
    @jitendranavagire3380 5 лет назад +1

    very good explanation

  • @mageshkarm459
    @mageshkarm459 3 года назад

    Thank you I understand logic

  • @hemamanohar15
    @hemamanohar15 2 года назад

    hi do you have any video about reversal string but in vertical way. i've tried so many ways but still no solution

  • @abc70574
    @abc70574 5 лет назад +1

    thanks for the help
    helped me lot

  • @Dunith_Munasinghe
    @Dunith_Munasinghe Год назад

    Thank you❤

  • @ajinkyarajurkar7457
    @ajinkyarajurkar7457 6 лет назад +1

    can u upload a video on "reverse" which is used everywhere in python like list and reverse operation on some other.. . . like reversing a list kind of

    • @AmulsAcademy
      @AmulsAcademy  6 лет назад

      Are you asking about the list.reverse method?

  • @t.gowthamarasu1258
    @t.gowthamarasu1258 5 лет назад +1

    Uday I too had a same thought

  • @kareemsaheb5438
    @kareemsaheb5438 4 года назад +1

    Good video mam

  • @manikandanarivazhagan5963
    @manikandanarivazhagan5963 Год назад

    Perfect 👌

  • @alphapotato480
    @alphapotato480 Год назад

    thank you.

  • @hksaggu1371
    @hksaggu1371 Год назад

    Very good thanku

  • @NasirHussain45
    @NasirHussain45 5 лет назад +1

    Thanku.... Very helpful

  • @The_WarMachine
    @The_WarMachine 3 года назад +1

    Nice 👍👍

  • @davidusharauli2227
    @davidusharauli2227 6 лет назад +1

    Thanks for reply. but why would for loop add m before a like m+a and not standard way a+m? this part is main part and your reply did not make it clear. if one puts 'amuls'[::-1] it makes sense to reverse. But why would x = i+var should loop from left to write rather than from right to left?

    • @AmulsAcademy
      @AmulsAcademy  6 лет назад +3

      hey,
      In the string concatenation,
      'a' + 'm' is 'am'
      'm' + 'a' is 'ma'
      for i in string:
      reversed_string = i +reversed_string
      here string is amuls,
      so first i value becomes 'a'
      so for loop executes for i='a',
      so ,
      reversed_string = i +reversed_string [ here,
      = 'a' + ""
      = 'a' [ i = 'a'
      [ reversed-string = '"" [ empty string ]
      Now reversed_string is 'a'
      so next i value becomes 'm'
      reversed_string = i +reversed_string
      = 'm' + 'a'
      ='ma'
      so reversed_string is 'ma' now.
      here we are doing,
      i+reversed_string
      so we get string is reversed order, that is 'sluma'
      if we write,
      reversed_string + i
      we will get the same string, that is "amuls"
      :)

    • @davidusharauli2227
      @davidusharauli2227 6 лет назад +1

      Thanks. I got it now. very nice.

  • @komaljaswani7497
    @komaljaswani7497 4 года назад

    Hi! Can you please make a video on the question: If a list in python has mix of strings & numbers, how will you make separate lists of strings & numbers? I saw ord() for getting ASCII code in python & chr() for converting ASCII codes to characters somewhere & tried to use them to get answer of this question but it did not work!! :(

  • @drsmahesan203
    @drsmahesan203 3 года назад +3

    Pythonic way to reverse a string is to use the slice operator
    E.g.
    str1 = "Amuls"
    rev_str = str1[ : : -1]
    :-)

  • @aditishukla6882
    @aditishukla6882 2 года назад

    thanks a lot

  • @swathiram1116
    @swathiram1116 5 лет назад +1

    Hai mam but getting syntax error like unindent does not match any indentation level what to do pls explain

    • @AmulsAcademy
      @AmulsAcademy  4 года назад

      I think you placed extra space before that statement.

  • @vijaysahal4556
    @vijaysahal4556 4 года назад +1

    nice 👍👍👍👍👍

  • @AliRaza-lv2kf
    @AliRaza-lv2kf 4 года назад

    whose version use you are?

  • @saurav031
    @saurav031 6 лет назад +2

    can you please add an example of how to reverse words of a given string using for loop. for example given string is "amuls academy of python" and i want the result string as "python of academy amuls". there are many ways to do this using split() and join() method but I want to do this using for loop. Please add a video on this. Thanks

    • @AmulsAcademy
      @AmulsAcademy  6 лет назад

      Try this:)
      sentence = input("enter the input string:")
      answer = ''
      temp = ''
      for char in sentence:
      if char != ' ' and char!='.':
      temp += char
      elif char=='.':
      answer = char + temp + ' ' + answer
      temp = ''
      else:
      answer = temp + ' ' + answer
      temp = ''
      answer = temp + ' ' + answer
      print(answer)

    • @saurav031
      @saurav031 6 лет назад

      Amuls Academy Thanks for your answer but can you please explain the code or atleast make one video tutorial on this.

  • @haicugolu4337
    @haicugolu4337 2 года назад

    very nice

  • @jumanjiwarlord
    @jumanjiwarlord 5 лет назад +2

    Let’s say the word is Dog. On the first iteration, the D is stored in the empty list. Now on the second iteration, we pick up the o. And we add the o to the list... the list already contains D but the second iteration will be added BEFORE the list. Now we have oD... and so forth... goD.
    The trick is in the concatenation. i + reversed_string is like o + D. It’s only confusing because we read left to right and this logic is going against our natural tendencies. We just can’t grasp that o being placed BEFORE the D in a concatenated string unless you understand concatenation (and building strings through concatenation).
    I hope this helps somebody. We’re all at different levels. Some people have mentioned the [::-1] technique. Splicing a list, I believe it’s called? That’s probably the easier way.

    • @jumanjiwarlord
      @jumanjiwarlord 5 лет назад +1

      Another thing, it doesn’t even have to be a word. After all, a computer doesn’t know what a Dog is. All it knows is characters. So instead of Dog, let’s use mZy. First iteration is m. m gets stored in an empty list. Second iteration is Z, then we add Z to the list to get Zm and so forth yZm. The point is we’re adding the characters BEFORE the stored list... as stated by i + reversed_string. That’s how it builds in reverse over the course of its iterations.