EN
Python - get last 3 characters of string
0 points
In this article, we would like to show you how to get the last 3 characters of a string in Python.
Quick solution:
xxxxxxxxxx
1
last_characters = string[-N:]
Get last N elements using []
operator:
xxxxxxxxxx
1
string[start_index: end_index]
or
xxxxxxxxxx
1
string[start_index: end_index: step]
Where:
start_index
- index position, from where it will start fetching the characters (default value is0
),end_index
- index position, where it will end fetching the characters (default value is end of the string),step
- interval between each character (default value is1
)
In this example, we get the last 3 characters of the "dirask"
string by index (-3
to end of the string
).
xxxxxxxxxx
1
string = "dirask"
2
last_characters = string[-3:]
3
print("The last 3 characters are:",last_characters)
Output:
xxxxxxxxxx
1
The last 3 characters are: ask