Python Index tutorial 4

WBOY
Release: 2024-07-26 11:26:34
Original
805 people have browsed it

Python Index tutorial 4

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
Copy after login

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
Copy after login

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

The above is the detailed content of Python Index tutorial 4. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!