DEV Community

Arokya Naresh
Arokya Naresh

Posted on

Python Index tutorial 22.07.2024

Index

String-Sequence of characters
each character have each position called as index
Index values starts from 0
Spaces inbetween each word also calculator
Indexing has positive and negative indexing
Negative indexing -1 starts from end of the character

Eg:
word='Message'

        M   e   s   s   a   g   e
Enter fullscreen mode Exit fullscreen mode

Positive Indexing 0 1 2 3 4 5 6
Negative Indexing -7 -6 -5 -4 -3 -2 -1

word[3]
s

len(word)
6

Slicing

string[startingindex:before value to end]

string[startingindex:before value to end:direction] -->this reverse the direction of the string

string[]-this prints the entire values

Eg:

        H   e   l   l   o   m   o   b   i   l   e
Enter fullscreen mode Exit fullscreen mode

Positive Indexing 0 1 2 3 4 5 6 7 8 9 10
Negative Indexing -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

msg='HelloMobile'
print(msg[2:5]) #postive index
print(msg[-5:-2]) #negative index
print(msg[::-1])reverse the string
print(msg[-2:-5:-1]) #reverse la print aagum
print(msg[5:])
print(msg[:-5])

o/p
llo
obi
elibomolleH
lib
Mobile
HelloM

Top comments (0)