Remove characters from the middle of a string in Python

avatar

Borislav Hadzhiev

Last updated: Jul 7, 2022

banner

Photo from Unsplash

Remove characters from the middle of a string in Python #

Use the str.replace() method to remove characters from the middle of a string, e.g. result_1 = my_str.replace('abc', ''). The str.replace() method returns a copy of the string with all occurrences of the substring replaced by the provided replacement.

main.py
my_str = 'avocado' # ✅ remove characters from string result_1 = my_str.replace('ca', '') print(result_1) # 👉️ 'avodo' # ---------------------------------------------- mid = len(my_str) // 2 print(mid) # 👉️ 3 # ✅ remove middle character from string result_2 = my_str[:mid] + my_str[mid+1:] print(result_2) # 👉️ 'avoado' # ---------------------------------------------- # ✅ remove more than 1 characters from middle of string result_3 = my_str[:mid] + my_str[mid+2:] print(result_3) # 👉️ 'avodo'

We used the str.replace() method to remove characters from the middle of a string.

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

main.py
my_str = 'avocado' result_1 = my_str.replace('ca', '') print(result_1) # 👉️ 'avodo'

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

Note that the method doesn't change the original string. Strings are immutable in Python.

We used an empty string for the new argument in order to remove the characters, rather than replace them.

You may also specify a value for the count argument if you only want to remove the first occurrence of the substring in the string.

main.py
print('hello'.replace('l', '', 1)) # 👉️ 'helo' print('hello'.replace('l', '')) # 👉️ 'heo'

To remove the middle characters from a string:

  1. Divide the string's length by 2 to get the index of the middle character
  2. Use string slicing to get a slice from index 0 up to the character.
  3. Use string slicing to get another slice starting at the character's index + N.
main.py
my_str = 'avocado' mid = len(my_str) // 2 print(mid) # 👉️ 3 # ✅ remove middle character from string result_2 = my_str[:mid] + my_str[mid+1:] print(result_2) # 👉️ 'avoado' # ---------------------------------------------- # ✅ remove more than 1 characters from middle of string result_3 = my_str[:mid] + my_str[mid+2:] print(result_3) # 👉️ 'avodo'

The syntax for string slicing is my_str[start:stop:step], where the start value is inclusive, and the stop value is exclusive.

The first example removes the middle character from the string.

The first slice my_str[:mid] starts at index 0 and goes up to, but not including the middle character.

The second slice my_str[mid+1:] starts at the index after the middle character and goes to the end of the string.

Notice that we had to add 1 to the start index in the second slice because the start index is inclusive, and we want to exclude the middle character from the result.

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.