Someone asked me this doubt during a Python webinar on how to get the last three characters of a string. I found several ways to accomplish this task using Python’s built-in string slicing and indexing functionalities. I will explain how to get the last 3 characters of a string in Python in this tutorial with examples and screenshots of executed example code.
Get the Last 3 Characters of a String in Python
Python provides various ways to get the last 3 characters of a string. Let us see some important methods.
Read How to Check if a Word is in a String in Python?
1. Use String Slicing
The simplest way to get the last 3 characters of a string in Python is by using string slicing with a negative index. Here’s an example:
zip_code = "90210"
last_3_digits = zip_code[-3:]
print(last_3_digits) Output:
210I have executed the above example code and added the screenshot below.

In this code snippet, we use [-3:] to slice the string starting from the third character from the end until the end of the string. The negative index -3 indicates that we want to start counting from the end of the string.
Check out Convert Binary to Decimal in Python
2. Use the len() Function and Positive Indexing
Another approach is to use the len() function to calculate the length of the string and then slice the last 3 characters using positive indexing. Here’s an example:
customer_id = "JohnDoe123"
last_3_chars = customer_id[len(customer_id)-3:]
print(last_3_chars)Output:
123I have executed the above example code and added the screenshot below.

In this example, len(customer_id) returns the length of the customer_id string. By subtracting 3 from the length, we get the starting index for slicing the last 3 characters.
Read How to Shuffle Characters in a String using Python?
3. Handle Edge Cases
When working with strings, it’s important to consider edge cases where the string might be shorter than the desired number of characters. To handle such cases, you can use conditionals to check the string length before slicing:
state_abbr = "CA"
if len(state_abbr) >= 3:
last_3_chars = state_abbr[-3:]
else:
last_3_chars = state_abbr
print(last_3_chars)Output:
CAI have executed the above example code and added the screenshot below.

In this example, we first check if the length state_abbr is greater than or equal to 3. If it is, we proceed with slicing the last 3 characters. Otherwise, we assign the entire state_abbr string to last_3_chars.
Check out How to Check if a String is a Valid UUID in Python?
Conclusion
In this tutorial, I have explained how to get the last 3 characters of a string in Python. I discussed some important methods like using string slicing, len() function and positive indexing, and how to handle edge cases.
You may also like to read:
- How to Generate a Random String of a Specific Length in Python?
- How to Convert a String to ASCII in Python?
- How to Swap Characters in a String Using Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.