This lecture you will learn python indexing and slicing!
Indexing and slicing with strings
Indexing
Course = ‘Python for Beginners’
print(course[0]) // will prints 0 character i.e “P” left to right
print(course[1]) // will prints 0 character i.e. “P” left to right
print(course[-1]) // will prints the Last character i.e “s” Last character
Note this facility is not available in other programming languages as well
print(course[-2]) // will prints the Last character i.e. “r” Last 2nd character
All you need to know about standard and reserved keywords of python programming language, by type a keyword of formmowing command to you know your python version support reserved keywords.
Slicing:
print(course[0:3]) // python interpreter will identify from 0 to index 3 it will prints
Output : Pyt
print(course[:3]) // it will treated as starting index is 0
print(course[1:3])
print(course[:]) // complete string will print here // 0 would be assume and end of the string will be assumed
Let’s take one more variable
more = course[:]
Print (more)
Output:
Python for Beginners
Exercise for you:
Name = ‘Python Student’
print(name[1:-1])
Tell me what is the Output of index printing
The output is first Index starts from 2nd character and Second Index says that remove Last character the range of characters will be print here
Output is: ython Studen
For reverse string
print(::-1)