0

Why doesn't it reverse the string when we put some numbers in start and stop parameters?

Example:

name = "i am a student"
print(name[0:14:-1])

does not reverse the string

David Buck
  • 3,752
  • 35
  • 31
  • 35

1 Answers1

0

In this line name[0:14:-1] in 14th position there is nothing in the string. slice notation is:

[ <first element to include> : <first element to exclude> : <step> ]

If you want to include the first element when reversing a list, leave the middle element empty, like this:

name = "i am a student"
s = name[::-1]
print(s) # output tneduts a ma i

please add your expected output with the post.

you can also check this post Python reverse-stride slicing

also this article https://www.jquery-az.com/ways-reverse-string-python-reversed-extended-slice-objects/

Tasnuva Leeya
  • 2,515
  • 1
  • 13
  • 21