-1

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
martineau
  • 119,623
  • 25
  • 170
  • 301
Gaurav
  • 13
  • 2

1 Answers1

2

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.

jpp
  • 159,742
  • 34
  • 281
  • 339