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,594
  • 33
  • 29
  • 34
  • what is your expected output? Are you intended to do partial reverse? – Tasnuva Sep 07 '20 at 04:05
  • 4
    Does this answer your question? [Python reverse-stride slicing](https://stackoverflow.com/questions/5798136/python-reverse-stride-slicing) – tornikeo Sep 07 '20 at 04:07

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
  • 2,105
  • 1
  • 9
  • 17