How to Get the Last 3 Characters of a String in Python?

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:

210

I have executed the above example code and added the screenshot below.

Get the Last 3 Characters of a String in Python

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:

123

I have executed the above example code and added the screenshot below.

How to Get the Last 3 Characters of a String in Python

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:

CA

I have executed the above example code and added the screenshot below.

Get the Last 3 Characters of a String in Python handle edge case

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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.