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

Bilel

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x