0
Extraterrestrials Challenge
Can someone please explain this line of code? It pertains to inputting a word, and outputting it in reverse order. I am unfamiliar with :: print(input()[::-1])
3 Respostas
+ 4
"string"[start:stop:step]
+ 6
M Heather Fleming ,
Lisa has shown the syntax of this "construct", that is called a slice. it enables us to make a "cutout" as a part or a complete object. slices can be applied to various python data types.
if the start value or the stop value are not given, it will take the complete object. the last parameter is the step.
the values used in slices are called index values. we can use not only positive indexes, but also negative indexes. positive indexes describes an index position that starts from the left side, negative indexes describes an index position that starts from the right side. so using -1 as a step value, will start from right side. this makes the output of the original object reverse.
positive index 0 1 2
lst = [1, 7, 4] to get the number 4 from this list: lst[2] or lst[-1]
negative index -3 -2 -1
for more information have a look to this short tutorial from the community section:
https://www.sololearn.com/learn/Python/2453/?ref=app