Reverse string Python












3















I would like to get input from a user and then to print the string reversed like this:

input:




hello






output:




o
ol
oll
olle
olleh




This is my code:



s = input()
for i in range(len(s) - 2, -1, -1):
print(s[:i:-1])


And output i receive is:




o
ol
oll
olle




I am constantly missing the last character. I tried many variations of the slicing. What am I missing?










share|improve this question



























    3















    I would like to get input from a user and then to print the string reversed like this:

    input:




    hello






    output:




    o
    ol
    oll
    olle
    olleh




    This is my code:



    s = input()
    for i in range(len(s) - 2, -1, -1):
    print(s[:i:-1])


    And output i receive is:




    o
    ol
    oll
    olle




    I am constantly missing the last character. I tried many variations of the slicing. What am I missing?










    share|improve this question

























      3












      3








      3








      I would like to get input from a user and then to print the string reversed like this:

      input:




      hello






      output:




      o
      ol
      oll
      olle
      olleh




      This is my code:



      s = input()
      for i in range(len(s) - 2, -1, -1):
      print(s[:i:-1])


      And output i receive is:




      o
      ol
      oll
      olle




      I am constantly missing the last character. I tried many variations of the slicing. What am I missing?










      share|improve this question














      I would like to get input from a user and then to print the string reversed like this:

      input:




      hello






      output:




      o
      ol
      oll
      olle
      olleh




      This is my code:



      s = input()
      for i in range(len(s) - 2, -1, -1):
      print(s[:i:-1])


      And output i receive is:




      o
      ol
      oll
      olle




      I am constantly missing the last character. I tried many variations of the slicing. What am I missing?







      python string python-3.x






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 19:05









      Strahinja RodicStrahinja Rodic

      145314




      145314
























          7 Answers
          7






          active

          oldest

          votes


















          2














          To get the full string reversed you must use s[::-1] omitting the first value.
          Since that doesn't fit into your iteration you'll have to use something like:



          s = input()

          for i in range(len(s) - 2, -1, -1):
          print(s[:i:-1])

          print(s[::-1])





          share|improve this answer
























          • I like this better than mine.

            – kabanus
            Nov 19 '18 at 19:20











          • I think this is the easiest solution of all. I really like it, and didn't really think about using s[::-1] in the end of the for loop, I was only trying to reverse it fully through iterations. Thanks! :)

            – Strahinja Rodic
            Nov 19 '18 at 19:37





















          4














          You must first obtain the substring and then reverse it:



          s = "hello"
          for i in range(len(s)-1, -1, -1):
          print(s[i:][::-1])


          Or:



          s = "hello"
          for i, _ in enumerate(s):
          print(s[i:][::-1])


          Or reverse the word and get the substring:



          s = "hello"
          for i, _ in enumerate(s):
          print(s[::-1][:i+1])





          share|improve this answer





















          • 1





            This is nice, but I don't think the double string slicing is strictly necessary.

            – jpp
            Nov 19 '18 at 19:16













          • @jpp Why do you think that double slicing is not right? What problems could it generate? :-)

            – eyllanesc
            Nov 19 '18 at 19:17






          • 2





            Double the operations :). I'm not saying it's wrong, it's just.. not necessary.

            – jpp
            Nov 19 '18 at 19:19






          • 1





            This is cool solution, but I don't think it's necessary to use double slicing

            – Strahinja Rodic
            Nov 19 '18 at 19:39



















          2














          Easiest perhaps is to iterate from -1 backwards:



          s = 'hello'
          for i in range(1, len(s)+1):
          print(s[-1: -i-1: -1])

          hello
          o
          ol
          oll
          olle
          olleh


          The way this works, you are slicing sequentially:





          • s[-1: -2: -1],


          • s[-1: -3: -1],...

          • s[-1: -len(s)-1: -1]






          share|improve this answer

































            0














            You were almost there. One thing was already addressed - you iterate to little (the -2). The second thing is you need to use None to include the 0th element in the print:



            >>> for i in range(len(s)-1, -1, -1):
            ... print(s[:i-1 if i else None :-1])





            share|improve this answer































              0














              I would do it this way:



              a = "Hello"
              for i in range(len(a)):
              print(a[-i-1])


              This way you are not dealing with string slices, only indexing the string and there isn't a bunch of extra numbers to figure out what they are doing.






              share|improve this answer
























              • Unfortunately, this won't work as you aren't defining any slices.

                – jpp
                Nov 19 '18 at 19:26











              • I see - I missed the requirement that the whole word is backwards. This method will print out each letter individually in the reverse order.

                – Anthony Herrera
                Nov 19 '18 at 19:34



















              0














              You could also simply reverse the complete thing and print it sliced from the start to save you some headache:



              b = "hello"   # your input
              h = b[::-1] # inverse all - so this is now olleh

              # list slicing is from:exclusive to - so simply add 1 to fit with the ranges values 0-4
              for k in ( h[0:i+1] for i in range(0,len(h)) ):
              print(k)


              Output:



              o
              ol
              oll
              olle
              olleh





              share|improve this answer































                0














                The very basic method to reverse a string s is s[::-1] (and save it
                in another variable, say s2).



                Then, to print consecutive rows with increasing part of this string (s2),
                you need a loop over range(1, len(s2)+1), stating the upper limit of s2
                (excluding).



                So the script can look like below:



                s = 'hello'
                s2 = s[::-1]
                for i in range(1, len(s2)+1):
                print(s2[:i])





                share|improve this answer























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381080%2freverse-string-python%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  7 Answers
                  7






                  active

                  oldest

                  votes








                  7 Answers
                  7






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  2














                  To get the full string reversed you must use s[::-1] omitting the first value.
                  Since that doesn't fit into your iteration you'll have to use something like:



                  s = input()

                  for i in range(len(s) - 2, -1, -1):
                  print(s[:i:-1])

                  print(s[::-1])





                  share|improve this answer
























                  • I like this better than mine.

                    – kabanus
                    Nov 19 '18 at 19:20











                  • I think this is the easiest solution of all. I really like it, and didn't really think about using s[::-1] in the end of the for loop, I was only trying to reverse it fully through iterations. Thanks! :)

                    – Strahinja Rodic
                    Nov 19 '18 at 19:37


















                  2














                  To get the full string reversed you must use s[::-1] omitting the first value.
                  Since that doesn't fit into your iteration you'll have to use something like:



                  s = input()

                  for i in range(len(s) - 2, -1, -1):
                  print(s[:i:-1])

                  print(s[::-1])





                  share|improve this answer
























                  • I like this better than mine.

                    – kabanus
                    Nov 19 '18 at 19:20











                  • I think this is the easiest solution of all. I really like it, and didn't really think about using s[::-1] in the end of the for loop, I was only trying to reverse it fully through iterations. Thanks! :)

                    – Strahinja Rodic
                    Nov 19 '18 at 19:37
















                  2












                  2








                  2







                  To get the full string reversed you must use s[::-1] omitting the first value.
                  Since that doesn't fit into your iteration you'll have to use something like:



                  s = input()

                  for i in range(len(s) - 2, -1, -1):
                  print(s[:i:-1])

                  print(s[::-1])





                  share|improve this answer













                  To get the full string reversed you must use s[::-1] omitting the first value.
                  Since that doesn't fit into your iteration you'll have to use something like:



                  s = input()

                  for i in range(len(s) - 2, -1, -1):
                  print(s[:i:-1])

                  print(s[::-1])






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 '18 at 19:19









                  user10673977user10673977

                  964




                  964













                  • I like this better than mine.

                    – kabanus
                    Nov 19 '18 at 19:20











                  • I think this is the easiest solution of all. I really like it, and didn't really think about using s[::-1] in the end of the for loop, I was only trying to reverse it fully through iterations. Thanks! :)

                    – Strahinja Rodic
                    Nov 19 '18 at 19:37





















                  • I like this better than mine.

                    – kabanus
                    Nov 19 '18 at 19:20











                  • I think this is the easiest solution of all. I really like it, and didn't really think about using s[::-1] in the end of the for loop, I was only trying to reverse it fully through iterations. Thanks! :)

                    – Strahinja Rodic
                    Nov 19 '18 at 19:37



















                  I like this better than mine.

                  – kabanus
                  Nov 19 '18 at 19:20





                  I like this better than mine.

                  – kabanus
                  Nov 19 '18 at 19:20













                  I think this is the easiest solution of all. I really like it, and didn't really think about using s[::-1] in the end of the for loop, I was only trying to reverse it fully through iterations. Thanks! :)

                  – Strahinja Rodic
                  Nov 19 '18 at 19:37







                  I think this is the easiest solution of all. I really like it, and didn't really think about using s[::-1] in the end of the for loop, I was only trying to reverse it fully through iterations. Thanks! :)

                  – Strahinja Rodic
                  Nov 19 '18 at 19:37















                  4














                  You must first obtain the substring and then reverse it:



                  s = "hello"
                  for i in range(len(s)-1, -1, -1):
                  print(s[i:][::-1])


                  Or:



                  s = "hello"
                  for i, _ in enumerate(s):
                  print(s[i:][::-1])


                  Or reverse the word and get the substring:



                  s = "hello"
                  for i, _ in enumerate(s):
                  print(s[::-1][:i+1])





                  share|improve this answer





















                  • 1





                    This is nice, but I don't think the double string slicing is strictly necessary.

                    – jpp
                    Nov 19 '18 at 19:16













                  • @jpp Why do you think that double slicing is not right? What problems could it generate? :-)

                    – eyllanesc
                    Nov 19 '18 at 19:17






                  • 2





                    Double the operations :). I'm not saying it's wrong, it's just.. not necessary.

                    – jpp
                    Nov 19 '18 at 19:19






                  • 1





                    This is cool solution, but I don't think it's necessary to use double slicing

                    – Strahinja Rodic
                    Nov 19 '18 at 19:39
















                  4














                  You must first obtain the substring and then reverse it:



                  s = "hello"
                  for i in range(len(s)-1, -1, -1):
                  print(s[i:][::-1])


                  Or:



                  s = "hello"
                  for i, _ in enumerate(s):
                  print(s[i:][::-1])


                  Or reverse the word and get the substring:



                  s = "hello"
                  for i, _ in enumerate(s):
                  print(s[::-1][:i+1])





                  share|improve this answer





















                  • 1





                    This is nice, but I don't think the double string slicing is strictly necessary.

                    – jpp
                    Nov 19 '18 at 19:16













                  • @jpp Why do you think that double slicing is not right? What problems could it generate? :-)

                    – eyllanesc
                    Nov 19 '18 at 19:17






                  • 2





                    Double the operations :). I'm not saying it's wrong, it's just.. not necessary.

                    – jpp
                    Nov 19 '18 at 19:19






                  • 1





                    This is cool solution, but I don't think it's necessary to use double slicing

                    – Strahinja Rodic
                    Nov 19 '18 at 19:39














                  4












                  4








                  4







                  You must first obtain the substring and then reverse it:



                  s = "hello"
                  for i in range(len(s)-1, -1, -1):
                  print(s[i:][::-1])


                  Or:



                  s = "hello"
                  for i, _ in enumerate(s):
                  print(s[i:][::-1])


                  Or reverse the word and get the substring:



                  s = "hello"
                  for i, _ in enumerate(s):
                  print(s[::-1][:i+1])





                  share|improve this answer















                  You must first obtain the substring and then reverse it:



                  s = "hello"
                  for i in range(len(s)-1, -1, -1):
                  print(s[i:][::-1])


                  Or:



                  s = "hello"
                  for i, _ in enumerate(s):
                  print(s[i:][::-1])


                  Or reverse the word and get the substring:



                  s = "hello"
                  for i, _ in enumerate(s):
                  print(s[::-1][:i+1])






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 19 '18 at 19:16

























                  answered Nov 19 '18 at 19:10









                  eyllanesceyllanesc

                  77.1k103156




                  77.1k103156








                  • 1





                    This is nice, but I don't think the double string slicing is strictly necessary.

                    – jpp
                    Nov 19 '18 at 19:16













                  • @jpp Why do you think that double slicing is not right? What problems could it generate? :-)

                    – eyllanesc
                    Nov 19 '18 at 19:17






                  • 2





                    Double the operations :). I'm not saying it's wrong, it's just.. not necessary.

                    – jpp
                    Nov 19 '18 at 19:19






                  • 1





                    This is cool solution, but I don't think it's necessary to use double slicing

                    – Strahinja Rodic
                    Nov 19 '18 at 19:39














                  • 1





                    This is nice, but I don't think the double string slicing is strictly necessary.

                    – jpp
                    Nov 19 '18 at 19:16













                  • @jpp Why do you think that double slicing is not right? What problems could it generate? :-)

                    – eyllanesc
                    Nov 19 '18 at 19:17






                  • 2





                    Double the operations :). I'm not saying it's wrong, it's just.. not necessary.

                    – jpp
                    Nov 19 '18 at 19:19






                  • 1





                    This is cool solution, but I don't think it's necessary to use double slicing

                    – Strahinja Rodic
                    Nov 19 '18 at 19:39








                  1




                  1





                  This is nice, but I don't think the double string slicing is strictly necessary.

                  – jpp
                  Nov 19 '18 at 19:16







                  This is nice, but I don't think the double string slicing is strictly necessary.

                  – jpp
                  Nov 19 '18 at 19:16















                  @jpp Why do you think that double slicing is not right? What problems could it generate? :-)

                  – eyllanesc
                  Nov 19 '18 at 19:17





                  @jpp Why do you think that double slicing is not right? What problems could it generate? :-)

                  – eyllanesc
                  Nov 19 '18 at 19:17




                  2




                  2





                  Double the operations :). I'm not saying it's wrong, it's just.. not necessary.

                  – jpp
                  Nov 19 '18 at 19:19





                  Double the operations :). I'm not saying it's wrong, it's just.. not necessary.

                  – jpp
                  Nov 19 '18 at 19:19




                  1




                  1





                  This is cool solution, but I don't think it's necessary to use double slicing

                  – Strahinja Rodic
                  Nov 19 '18 at 19:39





                  This is cool solution, but I don't think it's necessary to use double slicing

                  – Strahinja Rodic
                  Nov 19 '18 at 19:39











                  2














                  Easiest perhaps is to iterate from -1 backwards:



                  s = 'hello'
                  for i in range(1, len(s)+1):
                  print(s[-1: -i-1: -1])

                  hello
                  o
                  ol
                  oll
                  olle
                  olleh


                  The way this works, you are slicing sequentially:





                  • s[-1: -2: -1],


                  • s[-1: -3: -1],...

                  • s[-1: -len(s)-1: -1]






                  share|improve this answer






























                    2














                    Easiest perhaps is to iterate from -1 backwards:



                    s = 'hello'
                    for i in range(1, len(s)+1):
                    print(s[-1: -i-1: -1])

                    hello
                    o
                    ol
                    oll
                    olle
                    olleh


                    The way this works, you are slicing sequentially:





                    • s[-1: -2: -1],


                    • s[-1: -3: -1],...

                    • s[-1: -len(s)-1: -1]






                    share|improve this answer




























                      2












                      2








                      2







                      Easiest perhaps is to iterate from -1 backwards:



                      s = 'hello'
                      for i in range(1, len(s)+1):
                      print(s[-1: -i-1: -1])

                      hello
                      o
                      ol
                      oll
                      olle
                      olleh


                      The way this works, you are slicing sequentially:





                      • s[-1: -2: -1],


                      • s[-1: -3: -1],...

                      • s[-1: -len(s)-1: -1]






                      share|improve this answer















                      Easiest perhaps is to iterate from -1 backwards:



                      s = 'hello'
                      for i in range(1, len(s)+1):
                      print(s[-1: -i-1: -1])

                      hello
                      o
                      ol
                      oll
                      olle
                      olleh


                      The way this works, you are slicing sequentially:





                      • s[-1: -2: -1],


                      • s[-1: -3: -1],...

                      • s[-1: -len(s)-1: -1]







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 19 '18 at 19:21

























                      answered Nov 19 '18 at 19:15









                      jppjpp

                      98.1k2159109




                      98.1k2159109























                          0














                          You were almost there. One thing was already addressed - you iterate to little (the -2). The second thing is you need to use None to include the 0th element in the print:



                          >>> for i in range(len(s)-1, -1, -1):
                          ... print(s[:i-1 if i else None :-1])





                          share|improve this answer




























                            0














                            You were almost there. One thing was already addressed - you iterate to little (the -2). The second thing is you need to use None to include the 0th element in the print:



                            >>> for i in range(len(s)-1, -1, -1):
                            ... print(s[:i-1 if i else None :-1])





                            share|improve this answer


























                              0












                              0








                              0







                              You were almost there. One thing was already addressed - you iterate to little (the -2). The second thing is you need to use None to include the 0th element in the print:



                              >>> for i in range(len(s)-1, -1, -1):
                              ... print(s[:i-1 if i else None :-1])





                              share|improve this answer













                              You were almost there. One thing was already addressed - you iterate to little (the -2). The second thing is you need to use None to include the 0th element in the print:



                              >>> for i in range(len(s)-1, -1, -1):
                              ... print(s[:i-1 if i else None :-1])






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 19 '18 at 19:18









                              kabanuskabanus

                              11.4k31339




                              11.4k31339























                                  0














                                  I would do it this way:



                                  a = "Hello"
                                  for i in range(len(a)):
                                  print(a[-i-1])


                                  This way you are not dealing with string slices, only indexing the string and there isn't a bunch of extra numbers to figure out what they are doing.






                                  share|improve this answer
























                                  • Unfortunately, this won't work as you aren't defining any slices.

                                    – jpp
                                    Nov 19 '18 at 19:26











                                  • I see - I missed the requirement that the whole word is backwards. This method will print out each letter individually in the reverse order.

                                    – Anthony Herrera
                                    Nov 19 '18 at 19:34
















                                  0














                                  I would do it this way:



                                  a = "Hello"
                                  for i in range(len(a)):
                                  print(a[-i-1])


                                  This way you are not dealing with string slices, only indexing the string and there isn't a bunch of extra numbers to figure out what they are doing.






                                  share|improve this answer
























                                  • Unfortunately, this won't work as you aren't defining any slices.

                                    – jpp
                                    Nov 19 '18 at 19:26











                                  • I see - I missed the requirement that the whole word is backwards. This method will print out each letter individually in the reverse order.

                                    – Anthony Herrera
                                    Nov 19 '18 at 19:34














                                  0












                                  0








                                  0







                                  I would do it this way:



                                  a = "Hello"
                                  for i in range(len(a)):
                                  print(a[-i-1])


                                  This way you are not dealing with string slices, only indexing the string and there isn't a bunch of extra numbers to figure out what they are doing.






                                  share|improve this answer













                                  I would do it this way:



                                  a = "Hello"
                                  for i in range(len(a)):
                                  print(a[-i-1])


                                  This way you are not dealing with string slices, only indexing the string and there isn't a bunch of extra numbers to figure out what they are doing.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Nov 19 '18 at 19:25









                                  Anthony HerreraAnthony Herrera

                                  1




                                  1













                                  • Unfortunately, this won't work as you aren't defining any slices.

                                    – jpp
                                    Nov 19 '18 at 19:26











                                  • I see - I missed the requirement that the whole word is backwards. This method will print out each letter individually in the reverse order.

                                    – Anthony Herrera
                                    Nov 19 '18 at 19:34



















                                  • Unfortunately, this won't work as you aren't defining any slices.

                                    – jpp
                                    Nov 19 '18 at 19:26











                                  • I see - I missed the requirement that the whole word is backwards. This method will print out each letter individually in the reverse order.

                                    – Anthony Herrera
                                    Nov 19 '18 at 19:34

















                                  Unfortunately, this won't work as you aren't defining any slices.

                                  – jpp
                                  Nov 19 '18 at 19:26





                                  Unfortunately, this won't work as you aren't defining any slices.

                                  – jpp
                                  Nov 19 '18 at 19:26













                                  I see - I missed the requirement that the whole word is backwards. This method will print out each letter individually in the reverse order.

                                  – Anthony Herrera
                                  Nov 19 '18 at 19:34





                                  I see - I missed the requirement that the whole word is backwards. This method will print out each letter individually in the reverse order.

                                  – Anthony Herrera
                                  Nov 19 '18 at 19:34











                                  0














                                  You could also simply reverse the complete thing and print it sliced from the start to save you some headache:



                                  b = "hello"   # your input
                                  h = b[::-1] # inverse all - so this is now olleh

                                  # list slicing is from:exclusive to - so simply add 1 to fit with the ranges values 0-4
                                  for k in ( h[0:i+1] for i in range(0,len(h)) ):
                                  print(k)


                                  Output:



                                  o
                                  ol
                                  oll
                                  olle
                                  olleh





                                  share|improve this answer




























                                    0














                                    You could also simply reverse the complete thing and print it sliced from the start to save you some headache:



                                    b = "hello"   # your input
                                    h = b[::-1] # inverse all - so this is now olleh

                                    # list slicing is from:exclusive to - so simply add 1 to fit with the ranges values 0-4
                                    for k in ( h[0:i+1] for i in range(0,len(h)) ):
                                    print(k)


                                    Output:



                                    o
                                    ol
                                    oll
                                    olle
                                    olleh





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      You could also simply reverse the complete thing and print it sliced from the start to save you some headache:



                                      b = "hello"   # your input
                                      h = b[::-1] # inverse all - so this is now olleh

                                      # list slicing is from:exclusive to - so simply add 1 to fit with the ranges values 0-4
                                      for k in ( h[0:i+1] for i in range(0,len(h)) ):
                                      print(k)


                                      Output:



                                      o
                                      ol
                                      oll
                                      olle
                                      olleh





                                      share|improve this answer













                                      You could also simply reverse the complete thing and print it sliced from the start to save you some headache:



                                      b = "hello"   # your input
                                      h = b[::-1] # inverse all - so this is now olleh

                                      # list slicing is from:exclusive to - so simply add 1 to fit with the ranges values 0-4
                                      for k in ( h[0:i+1] for i in range(0,len(h)) ):
                                      print(k)


                                      Output:



                                      o
                                      ol
                                      oll
                                      olle
                                      olleh






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 19 '18 at 19:25









                                      Patrick ArtnerPatrick Artner

                                      23.4k62343




                                      23.4k62343























                                          0














                                          The very basic method to reverse a string s is s[::-1] (and save it
                                          in another variable, say s2).



                                          Then, to print consecutive rows with increasing part of this string (s2),
                                          you need a loop over range(1, len(s2)+1), stating the upper limit of s2
                                          (excluding).



                                          So the script can look like below:



                                          s = 'hello'
                                          s2 = s[::-1]
                                          for i in range(1, len(s2)+1):
                                          print(s2[:i])





                                          share|improve this answer




























                                            0














                                            The very basic method to reverse a string s is s[::-1] (and save it
                                            in another variable, say s2).



                                            Then, to print consecutive rows with increasing part of this string (s2),
                                            you need a loop over range(1, len(s2)+1), stating the upper limit of s2
                                            (excluding).



                                            So the script can look like below:



                                            s = 'hello'
                                            s2 = s[::-1]
                                            for i in range(1, len(s2)+1):
                                            print(s2[:i])





                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              The very basic method to reverse a string s is s[::-1] (and save it
                                              in another variable, say s2).



                                              Then, to print consecutive rows with increasing part of this string (s2),
                                              you need a loop over range(1, len(s2)+1), stating the upper limit of s2
                                              (excluding).



                                              So the script can look like below:



                                              s = 'hello'
                                              s2 = s[::-1]
                                              for i in range(1, len(s2)+1):
                                              print(s2[:i])





                                              share|improve this answer













                                              The very basic method to reverse a string s is s[::-1] (and save it
                                              in another variable, say s2).



                                              Then, to print consecutive rows with increasing part of this string (s2),
                                              you need a loop over range(1, len(s2)+1), stating the upper limit of s2
                                              (excluding).



                                              So the script can look like below:



                                              s = 'hello'
                                              s2 = s[::-1]
                                              for i in range(1, len(s2)+1):
                                              print(s2[:i])






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 19 '18 at 19:28









                                              Valdi_BoValdi_Bo

                                              4,7202815




                                              4,7202815






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381080%2freverse-string-python%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                                                  ComboBox Display Member on multiple fields

                                                  Is it possible to collect Nectar points via Trainline?