Extraterrestrials Challenge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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])

3rd Aug 2022, 6:52 PM
M Heather Fleming
M Heather Fleming - avatar
3 Answers
+ 4
"string"[start:stop:step]
3rd Aug 2022, 6:57 PM
Lisa
Lisa - avatar
+ 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
4th Aug 2022, 12:42 PM
Lothar
Lothar - avatar
+ 1
Lisa Lothar thank you!!
4th Aug 2022, 4:31 PM
M Heather Fleming
M Heather Fleming - avatar