Remove Last Character from String in Python

Recently, I was working on a project where I had to clean up user input before storing it in a database. One of the common issues I faced was that many strings had an extra character at the end, sometimes a comma, sometimes a period, and sometimes just a trailing space.

The challenge was simple: I needed to remove the last character from these strings. But as I dug deeper, I realized there are multiple ways to do this in Python, and each method has its strengths.

In this tutorial, I will share five practical methods I use to remove the last character from a string in Python. These methods are simple, efficient, and easy to apply in real-world scenarios.

Method 1 – Use String Slicing

The most common way I remove the last character from a string is by using slicing. Python strings are sequences, so slicing works perfectly.

Here’s the code:

text = "New York,"
cleaned_text = text[:-1]  # Removes the last character
print("Original:", text)
print("After removing last character:", cleaned_text)

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

python remove last character from string

Explanation:

  • text[:-1] means take all characters from the beginning up to (but not including) the last character.
  • This method is fast and works for any string.

I often use this when I’m cleaning up CSV data where every line ends with an extra comma.

Method 2 – Use rstrip()

Another method I use is the rstrip() function in Python. This is useful when the last character is a specific one, like a period, comma, or whitespace.

text = "California."
cleaned_text = text.rstrip(".")  # Removes trailing period
print("Original:", text)
print("After removing last character:", cleaned_text)

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

remove last character from string python

Explanation:

  • rstrip() removes trailing characters (not just one).
  • If the last character is always the same (like . or ,), this method is very easy.

I use this method when cleaning up addresses in a dataset where users often end with a period.

Method 3 – Use String Concatenation

Sometimes, I prefer to split the Python string into two parts and then join them back without the last character.

text = "Texas!"
cleaned_text = text[:len(text)-1] + ""
print("Original:", text)
print("After removing last character:", cleaned_text)

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

python string remove last character

Explanation:

  • Here, I take all characters except the last one and concatenate them into a new string.
  • This is similar to slicing, but it makes the logic very explicit.

This method is useful when I want to emphasize readability in my code for beginners.

Method 4 – Use Regular Expressions (re Module)

When working with more complex text data, I sometimes use regex.

import re

text = "Washington?"
cleaned_text = re.sub(r'.$', '', text)
print("Original:", text)
print("After removing last character:", cleaned_text)

Explanation:

This method is powerful when I’m working with logs or text parsing, where I need flexibility.

Method 5 – Use removesuffix() (Python 3.9+)

In Python 3.9 and above, removesuffix() is a clean and modern way to remove a known suffix.

text = "Florida/"
cleaned_text = text.removesuffix("/")
print("Original:", text)
print("After removing last character:", cleaned_text)

Explanation:

  • This method is best when you know exactly which character you want to remove.
  • It’s safer than slicing because it won’t accidentally remove the wrong character if the string doesn’t end with the expected suffix.

I use this when dealing with file paths or URLs where I need to remove a trailing slash.

Practical Example: Clean Up a List of City Names

Let’s put all this together with a practical example. Imagine I have a list of US city names collected from user input, and some of them have extra punctuation at the end.

cities = ["New York,", "Los Angeles.", "Chicago!", "Houston/", "Phoenix?"]

# Clean all cities using slicing
cleaned_cities = [city[:-1] for city in cities]
print("Original list:", cities)
print("Cleaned list:", cleaned_cities)

Output:

Original list: ['New York,', 'Los Angeles.', 'Chicago!', 'Houston/', 'Phoenix?']
Cleaned list: ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']

This is a real-world case where removing the last character makes the dataset clean and ready for analysis.

Things to Keep in Mind

  • Empty Strings: If the string is empty, slicing or regex will still work, but you should handle it to avoid unexpected results.
  • Performance: For large datasets, slicing is the fastest method.
  • Python Version: If you’re using removesuffix(), make sure you’re on Python 3.9 or later.
  • Specific vs Generic Removal: If you always know the last character (like a slash in URLs), use removesuffix() or rstrip(). If not, slicing is safer.

While Python does not have a single built-in function dedicated to removing the last character from a string, we have multiple approaches that are simple and effective.

I use slicing most of the time because it’s short and efficient. But when I’m working with specific characters like commas, periods, or slashes, I find rstrip() and removesuffix() more reliable.

Both beginners and experienced developers will find these methods useful, and once you practice them a few times, you’ll know exactly which one to use in different situations.

You may also 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.