In this Python tutorial, we are going to discuss how to remove the last character from a string in Python. Also, we will demonstrate interesting techniques to remove the last character from a string in Python with examples:
Python string remove last character
Sometimes, when working with strings in Python, you might find it necessary to remove the last character from a string. This could be due to the presence of an extra character, the need to truncate information, or some other type of data cleaning.
There are several ways to achieve this in Python. So, let’s discuss each of the ways in the detail with example:
Method 1: Using Python’s Slice Notation
The easiest and most Pythonic way to remove the last character from a string is to use Python’s slice notation.
Let’s say we have a string that represents the phrase of a famous American figure, Neil Armstrong: “That’s one small step for man, one giant leap for mankind.” If we want to remove the period at the end example is as follows:
s = "That's one small step for man, one giant leap for mankind."
s = s[:-1]
print(s)
In this code, the Python [:-1]
slice notation means “start at the beginning and go up to but not including the last element.” This will return all characters except the last one.
The output of this script would be:
Method 2: Using Python’s rstrip() Function
While slicing is a handy tool, Python’s built-in string method rstrip()
can also be used to remove the last character from a string.
However, it’s important to note that rstrip()
is designed to remove trailing characters. In the case where the last character is not known, or can vary, this method is not the most suitable.
Here we’ll use a well-known quote from Abraham Lincoln’s Gettysburg Address:
s = "Government of the people, by the people, for the people"
s = s.rstrip(s[-1])
print(s)
In this example, Python rstrip(s[-1])
removes the trailing character that matches the last character of the string s
.
The output of this script would be:
Method 3: Using the str[:-1]
Approach
This method is nearly identical to the slice notation method but instead uses a string constructor for clarity. If you’re dealing with an object that might not be a string, this method is a safer bet.
In this example, we’ll use a common American saying:
s = "The early bird catches the worm"
s = str(s[:-1])
print(s)
In this code, the Python str(s[:-1])
operation constructs a new string that includes every character except the last.
The output of this script would be:
Conclusion
In Python, removing the last character from a string can be accomplished in several ways. The slice notation method is typically the most straightforward and Pythonic approach.
However, rstrip()
may be more suitable if you’re dealing with trailing characters, and str[:-1]
offers a safe approach when the type of your data isn’t guaranteed.
You may also like to read the following Python tutorials.
- Armstrong number in Python
- Python naming conventions
- Python dictionary append
- Python program for finding greatest of 3 numbers
- Strip function in Python string
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.