I created a string variable stro
.
How will the slicing stro[7:][-6]
work on stro
?
stro = "Python is fun"
print(stro[7:][-6])
# output: i
I created a string variable stro
.
How will the slicing stro[7:][-6]
work on stro
?
stro = "Python is fun"
print(stro[7:][-6])
# output: i
You are slicing, then indexing:
stro = "Python is fun"
x = stro[7:] # 'is fun'
y = x[-6] # 'i'
Since strings are immutable, both x
and y
are new strings rather than a "view" of an object. Thus stro[7:]
returns 'is fun'
and indexing the 6th last character returns 'i'
.
The syntax is similar to lists: see Understanding Python's slice notation.