Remove the last character from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 13, 2022

banner

Photo from Unsplash

Remove the last character from a String in Python #

Use string slicing to remove the last character from a string, e.g. result = my_str[:-1]. The new string will contain a slice of the original string with the last character removed.

main.py
my_str = 'avocado' result_1 = my_str[:-1] print(result_1) # 👉️ 'avocad' result_2 = my_str.rstrip(my_str[-1]) print(result_2) # 👉️ 'avocad' result_3 = my_str.rstrip('o') print(result_3) # 👉️ 'avocad' # ✅ if you need to remove the last word from a string my_str_2 = 'hello world' result_4 = my_str_2.rsplit(' ', 1)[0] print(result_4) # 👉️ 'hello'

The first example uses string slicing to remove the last character from a string.

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The slice my_str[:-1] starts at the character at index 0 and goes up to, but not including the last character in the string.

main.py
print('test'[:-1]) # 👉️ 'tes' print('world'[:-1]) # 👉️ 'worl'

If you use this approach with an empty string, you will get an empty string back.

main.py
print(''[:-1]) # 👉️ ''

No error is raised even though the string in the example is empty.

Alternatively, you can use the str.rstrip() method.

Use the str.rstrip() method to remove the last character from a string, e.g. result = my_str.rstrip(my_str[-1]). The str.rstrip() method will return a new string with the specified trailing character removed.

main.py
my_str = 'avocado' result_1 = my_str.rstrip(my_str[-1]) print(result_1) # 👉️ 'avocad' result_2 = my_str.rstrip('o') print(result_2) # 👉️ 'avocad'

The first example uses a negative index of -1 to get the last character of the string.

Negative indices can be used to count backwards, e.g. my_str[-1] returns the last character in the string and my_str[-2] returns the second-to-last character.

The str.rstrip method takes a string containing characters as an argument and returns a copy of the string with the specified trailing characters removed.

The method does not change the original string, it returns a new string. Strings are immutable in Python.

Note that accessing the string at an index that is out of range would raise an error.

main.py
my_str = '' # ⛔️ IndexError: string index out of range result_1 = my_str.rstrip(my_str[-1]) print(result_1) # 👉️ 'avocad'

If you need to handle this scenario, use a try/except statement.

main.py
my_str = '' try: result_1 = my_str.rstrip(my_str[-1]) print(result_1) except IndexError: print('The specified string is empty')
Note that the str.rstrip() method doesn't necessarily remove a single character.

The method removes all occurrences and combinations of the specified character from the end of the string.

Use the str.rsplit() method if you need to remove the last word from a string.

main.py
my_str = 'hello world' result = my_str.rsplit(' ', 1)[0] print(result) # 👉️ 'hello'

We split the string on a space, a single time, from the right and returned the first list item.

You can also use the str.removesuffix() method to remove the last character from a string.

main.py
my_str = 'avocado' result = my_str.removesuffix('o') print(result) # 👉️ 'avocad'

The str.removesuffix method checks if the string ends with the specified suffix and if it does, the method returns a new string excluding the suffix, otherwise it returns a copy of the original string.

The removesuffix method can also be used to remove more than 1 character from the end of the string.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.