-1

Can anybody explain me this: s[81:67:-1]

When s is "this is me asking a question on stackoverflow.com and hoping to get an answer that will help me further"

grmbl
  • 2,062
  • 2
  • 25
  • 52

2 Answers2

2

It is accessing string in reverse. From 81st character, till 67th character. The step value is -1. If you specify a positive value, you ll get nothing.

print s[81:67:-1]
print s[81:67:1]

Output

taht rewsna na

You can read more about it here. It is called slicing notation.

thefourtheye
  • 221,210
  • 51
  • 432
  • 478
1

You are accessing the element from position 81 to 67 but in reverse order (:-1 achieve this)

So, you are iterating over elements from position 67 to 81.

user278064
  • 9,778
  • 1
  • 32
  • 46