0

I am a new to python, trying to reverse a string but not working why ?

name='Sam Floyd'
reverse = name[8:-1:-1]
Vinay Hegde
  • 1,424
  • 1
  • 10
  • 23
  • 1
    Your start index is `8` which is correct because the string ends at index 8 but your stop index is index `-1` which is the last index in the string(same as index 8). If you change the stop index to `0`, the slicing terminates at index 1. You need to remove the stop index so that the slicing terminates at the begining of the string. Then you have: `reverse = name[8::-1]` or in short `reverse = name[::-1]` Please refer to this [link](https://stackoverflow.com/questions/509211/understanding-slice-notation) – DevD'Silva Jun 07 '20 at 09:45

1 Answers1

0

It is simpler to use that one :)

name='Sam Floyd'
reverse = name[::-1]
Vivian
  • 335
  • 2
  • 10