New course! Every coder should learn Generative AI!
Try a free lesson0
s="Welcome"
s="Welcome" print(s[2::-4]) Output: l It's an index, print from the second in the index (l) to the end (e) and count by -4? Am I misreading this? Also, does -1 start from "e" the last character in the string or "m" the second to last character in the string (I think I've seen both ways before). Thanks for any help.
3 Answers
+ 1
You're reading it correctly, but the output is the letter l, not 1 (one).
If the first number is -1 it starts from the last character ("e" in this case). If the second number is -1 it ends at the second to last character ("m" in this case).
0
-2 -1 1 2 3 4
w e l c o m e
"-4" - это slice step который отсчитываеться от "l" в левую сторону. Так как "-4" выходит за пределы индекса относительно "l", то и выводит только s[2] - "l".
"-4" is a slice step that counts from "l" to the left. Since "-4" is outside the index with respect to "l", it only outputs s [2] - "l".
0
Thanks Diego! BTW, I edited my post (changed "1" to "l":)).