Slicing Strings in Python

What is String Slicing in Python:

Slicing a String is to specify a range of characters of a String, here is an example:

a = "Hello, its time to learn, LearDuty"
print(a[2:9])

Output:
llo, it



Slice From the Start in Python

a = "Hello, its time to learn, LearDuty"
print(a[:9])

Output:
Hello, it



Slice To the End in Python

a = "Hello, its time to learn, LearDuty"
print(a[2:])

Output:
llo, its time to learn, LearDuty



Slicing with Negative Index in Python

Get the last two characters in a String in Python:

a = "Hello, its time to learn, LearDuty"
print(a[-2:])

Output:
ty

Bilel Ameur

Enthusiastic Network Engineer specializing in Cisco ACI, passionate about solving challenges. A lifelong learner who loves gaining and sharing knowledge. Profile: https://www.linkedin.com/in/bilel-ameur-71116b2b5
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x