Python strings come with a number of useful methods and techniques to help you with string manipulation. In this tutorial, we will look at how to remove the last character from a string in Python with the help of some examples.
How to remove the final character of a string?
The simplest way to remove the last character from a string is to slice the string from the first character to the second last character. To slice a string, s from index i
to index j
(but not including the character at index j
), you can use the notation s[i:j]
.
Strings (and other iterables) in Python are indexed starting from 0. This means that the last character in the string is present at the index len(s) - 1
. Let’s now use the two indices to slice the string.
# create a string s = "Boomerang" # remove the last character s[0:len(s)-1]
Output:
'Boomeran'
You can see that the resulting string has the last character from the original string removed. When slicing from the start of the string, you don’t need to explicitly specify the starting index in the slice.
# remove the last character s[:len(s)-1]
Output:
'Boomeran'
We get the same result as above.
Iterables like strings, lists, etc. in Python work with both positive and negative indexes. The negative index starts from the end of the iterable. For example, item at index -1
is the last item in the iterable, item at the -2
is the second last item and so on.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
You can use the -1
index to get the last character in the string.
# create a string s = "Boomerang" # the last character s[-1]
Output:
'g'
We can use this index in the slice to remove the last character from the string.
# create a string s = "Boomerang" # remove the last character s[:-1]
Output:
'Boomeran'
We get the same result as above. Here we slice the string from the start till the last character (but not including it).
There are other methods as well to remove the last character from a string in Python. For example, you can use the string rstrip()
function which by default removes trailing whitespace characters but can be modified to remove any character from the end of a string. Pass the character or the characters to be removed from the end of the string as an argument.
# create a string s = "Boomerang" # remove the last character s.rstrip(s[-1])
Output:
'Boomeran'
We get the string with the last character removed. Here we pass the last character in s, s[-1]
as an argument to the rstrip()
function to indicate that we want to remove this specific character from the end of the string.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.