How to get last 4 characters of a string in Python
In this tutorial, we are going to learn about how to get the last 4 characters of a string in Python.
Consider, we have the following string:
str = "abcdefgh"
To access the last 4 characters of a string in Python, we can use the subscript syntax [ ]
by passing -4:
as an argument to it.
-4:
is the number of characters we need to extract from the end position of a string.
Here is an example:
str = "abcdefgh"
lastFour = str[-4:]
print(lastFour)
Output:
"efgh"