Python – Remove Character From String

In this tutorial we will look at how to remove a character from a string in Python. But before we proceed, a quick question –

Can you remove characters from strings in Python?

Not exactly. Strings are immutable in python and hence cannot be changed. You can, however, create a new string with the required character removed and this is exactly what we will cover in this tutorial. Let’s look at the different use-cases of removing characters from strings with the help of examples.

If you want to remove all occurrences of a character in a string in Python, you can use the string replace() function or string translate() function with a translation table. Both these methods can return a new string with all the occurrences of the character removed. Let’s look at them through examples –

The string replace() function is used to replace the occurrences of a substring by a different substring. To remove the occurrence of a character, you can replace it with an empty string.

s = "abbcccdddd"
# reomve 'c' from s
print(s.replace("c", ""))

Output:

abbdddd

You can see that the returned string has all the occurrences of “c” removed.

You can also create a translation table and use str.translate() to remove occurrences of a character in a string. A translation table is basically a mapping used to replace strings with appropriate values.

s = "abbcccdddd"
# reomve 'c' from s
print(s.translate({ord("c"): None}))

Output:

abbdddd

You can see that all the occurrences of “c” have been removed in the returned string. Note that we passed a translation table as a dictionary mapping the Unicode for the character “c” to None to the translate() function. The ord() function returns an integer representing the Unicode code of the given character.

For more on the translate() function, refer to its documentation.

If you want to remove a character based on its index in a string, you can use a combination of slicing and string concatenation to remove that character in a new string. Here, we create the substrings to the left and right of the character by slicing the original string and then concatenate the two to create a new string with the character removed.

s = "abcdef"
# remove charater at the 2nd index
i = 2
new_s = s[:i] + s[i+1:]
# print the string
print(new_s)

Output:

abdef

You can see that the resulting string has the character “c” at index 2 from the original string removed. Note that this method only removes the character at a specific index and does not remove all the occurrences of the particular character. For example –

s = "abcdefc"
# remove charater at the 2nd index
i = 2
new_s = s[:i] + s[i+1:]
# print the string
print(new_s)

Output:

abdefc

You can see that only the character at index 2, which happened to be “c” was removed. Other characters including other occurrences of “c”, were not removed.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.