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:
last_characters = string[-N:]
Overview
Get last N elements using []
operator:
string[start_index: end_index]
or
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
)
Practical example
1. Get the last 3 characters of a string
In this example, we get the last 3 characters of the "dirask"
string by index (-3
to end of the string
).
string = "dirask"
last_characters = string[-3:]
print("The last 3 characters are:",last_characters)
Output:
The last 3 characters are: ask