Replace the last character in a String in Python

avatar

Borislav Hadzhiev

Last updated: Sep 23, 2022

banner

Photo from Unsplash

Replace the last character in a String in Python #

Use string slicing to replace the last character in a string, e.g. new_str = my_str[:-1] + '_'. The slice of the string excludes the last character, so we can append the replacement character using the addition (+) operator.

main.py
my_str = 'bobbyhadz.com' new_str = my_str[:-1] + '_' print(new_str) # 👉️ bobbyhadz.co_

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

main.py
my_str = 'bobbyhadz.com' print(my_str[:-1]) # 👉️ bobbyhadz.co

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 last step is to use the addition (+) operator to append the replacement string.

If the replacement value in your case is not of type string, use the str() class to convert it to a string.

main.py
my_str = 'bobbyhadz.com' new_str = my_str[:-1] + str(123) print(new_str) # 👉️ bobbyhadz.co123

The values on the left and right-hand sides of the addition (+) operator need to be of compatible types.

Alternatively, you can use the re.sub() method.

Replace the last character in a String using re.sub() #

Use the re.sub() method to replace the last character in a string, e.g. new_str = re.sub(r'm$', 'new', my_str). The re.sub() method can be passed a pattern that is only matched at the end of the string and will replace the last character with the provided replacement.

main.py
import re my_str = 'bobbyhadz.com' new_str = re.sub(r'm$', '_', my_str) print(new_str) # 👉️ bobbyhadz.co_

The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

If the pattern isn't found, the string is returned as is.

The first argument we passed to the re.sub() method is the pattern we want to match in the string and the second is the replacement for the match.

The dollar sign $ matches the end of the string, so the specified character is only replaced if it's at the end of the string.

Alternatively, you can use the list() class.

Replace the last character in a String using list() #

To replace the last character in a string:

  1. Use the list() class to convert the string to a list of characters.
  2. Update the value of the last element in the list.
  3. Use the str.join() method to join the list into a string.
main.py
my_str = 'bobbyhadz.com' list_of_chars = list(my_str) # 👇️ ['b', 'o', 'b', 'b', 'y', 'h', 'a', 'd', 'z', '.', 'c', 'o', 'm'] print(list_of_chars) list_of_chars[-1] = '_' new_str = ''.join(list_of_chars) print(new_str) # 👉️ bobbyhadz.co_

We used the list() class to convert the string to a list of characters.

main.py
my_str = 'bobbyhadz.com' # 👇️ ['b', 'o', 'b', 'b', 'y', 'h', 'a', 'd', 'z', '.', 'c', 'o', 'm'] print(list(my_str))

As opposed to strings, lists are mutable, so we can directly update the value of a list element at a specific index.

The last step is to use the str.join() method to join the list into a string.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

We used an empty string separator to join the list into a string without a delimiter.

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.