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

As a data scientist working with US customer data, I recently encountered a problem where I needed to get the last four digits of US zip codes for analysis. After searching for solutions, I found two ways to accomplish this task using Python’s built-in string slicing and indexing functionalities. I will help you learn how to get the last 4 characters of a string in Python In this tutorial with examples and screenshots of executed example code.

Get the Last 4 Characters of a String in Python

Let’s say you have a list of customer names from the USA, and you want to extract the last 4 characters of each name for some specific analysis. For example:

names = ["John Doe", "Jane Smith", "Michael Johnson", "Emily Davis"]

You need a way to efficiently extract the last 4 characters from each name in the list.

Read How to Check if a Word is in a String in Python?

Solution 1: Use String Slicing

The simplest and most concise way to get the last 4 characters of a string in Python is by using string slicing. Python allows you to extract a portion of a string by specifying the start and end indices. To get the last 4 characters, you can use negative indexing.

Here’s an example:

name = "Emily Davis"
last_4_chars = name[-4:]
print(last_4_chars) 

Output:

avis

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

Last 4 Characters of a String in Python

In this code, name[-4:] means start from the 4th character from the end of the string and go until the end. The colon (:) After -4 indicates that we want to include all characters until the end of the string.

To apply this solution to our list of names, you can use a loop or a list comprehension:

names = ["John Doe", "Jane Smith", "Michael Johnson", "Emily Davis"]
last_4_chars_list = [name[-4:] for name in names]
print(last_4_chars_list) 

Output:

[' Doe', 'mith', 'nson', 'avis']

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

Get the Last 4 Characters of a String in Python

Read How to Shuffle Characters in a String using Python?

Solution 2: Use the len() Function

Another approach to get the last 4 characters of a string is by using the len() function to calculate the length of the string and then slice accordingly.

Here’s an example:

name = "Michael Johnson"
length = len(name)
last_4_chars = name[length - 4:]
print(last_4_chars) 

Output:

nson

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

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

This code len(name) gives the total length of the string and length - 4 calculates the starting index for the last 4 characters.

Read How to Check if a String is a Valid UUID in Python?

Handle Edge Cases

When working with strings, it’s important to consider edge cases. What if the string has fewer than 4 characters? In such cases, you can gracefully modify the solutions to handle shorter strings.

Here’s an example using string slicing with a conditional check:

name = "Amy"
last_4_chars = name[-4:] if len(name) >= 4 else name
print(last_4_chars) 

Output:

Amy

In this code, we check if the length of the string is greater than or equal to 4. If it is, we extract the last 4 characters using string slicing. If the string is shorter than 4 characters, we return the entire string as is.

Read How to Generate a Random String of a Specific Length in Python?

Conclusion

In this tutorial, we explored two methods to get the last 4 characters of a string in Python: string slicing and the len() function. String slicing provides a concise and efficient way to extract a specific portion of a string, while the len() function allows you to calculate the length of the string and slice accordingly.

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.