Last answered:

29 Mar 2020

Posted on:

28 Mar 2020

1

problem in python programming

while doing assigned question from python boot camp i came through a problem of reversing an string solution given is -  print('Enter a string')
x=input()
y=x[::-1]
print(y) i couldn't understand the y=x[::-1] command. can you please explain more about it.(wasn't able to find this command on internet so if you have any link please share, i will refer that) Thank you!:) 
1 answers ( 0 marked as helpful)
Instructor
Posted on:

29 Mar 2020

0

Hi Prafful
thanks for reaching out!
This particular example is used to reverse a string using a slicer. The -1 is the step, so at each iteration we're going back one element at a time. Here we haven't specified the length of the interval to be reversed, so by default the whole string is reversed.
Here is a link with more on the topic of slicing:
https://www.educative.io/edpresso/how-do-you-reverse-a-string-in-python

Best,
Eli

Submit an answer