Python – Remove All Occurrences Of A Character In A String

In this tutorial, you will learn how you can remove all occurrences of a character in a string. You can use different inbuilt methods present in Python to delete the characters from a string.

There are a total of five methods that you can use to solve the problem above, and I’ll show you each one with examples.

1. Using str.replace Method

The first method is to use str.replace(). This method requires two arguments: the first is the character to be removed or deleted, and the second is a blank character.

Let us see in the below example code the usage of str.replace function to remove occurrences of a character in a string in Python.

#Initializing a String Element
newString = "xCoduberx Visitedx"

#using replace method to remove the x from 
#the above string
newString = newString.replace("x", "")

#printing the replaced string
print(newString)

Output:

Coduber Visited

As you can see in the above code, I was able to remove the unwanted character x from the above string using the replace function. You can also use the same function to remove the characters if you want.

This method is the fastest method among all the methods discussed in this tutorial.

2. Using String Comprehension Join Function

You can use the string comprehension function known as join. Using the join function you can regenerate the string omitting or deleting the unwanted characters.

Let us see in the below example code the usage of the join function to remove all the occurrences of a character in a string.

#Initializing a String Element
newString = "xCoduberx Visitedx"

#Regenerating the above string using Join
#Without using x
newStr = "".join([newChar for newChar in newString if newChar != "x"])

#printing the replaced string
print(newStr)

Output:

Coduber Visited

As demonstrated in the preceding code, I was able to remove all occurrences of a character from a string by using the join function. The join method iterates over all the characters in a string and replaces them with an empty string if they are not equal to “x”.

3. Using Regex sub function To Remove All Occurrences Of A Character In A String

Regex is the third method for removing characters from a string. Regex is a Python external library that must be imported before it can be used. I will be using the sub method of regex library that is used to substitute the character in a string.

Let us see in the below example code the usage of regex to delete all occurrences of a character in a string in Python.

#importing Regex
import re as regex

#Initializing a String Element
newString = "xCoduberx Visitedx and xTested"

#Using regex recreate the string to remove
#x from the above string.
newStr = regex.sub(r'x','', newString)

#printing the replaced string
print(newStr)

Output:

Coduber Visited and Tested

As demonstrated in the preceding code, I was able to successfully delete all instances of the character x from the given string by utilizing the regex sub function. This function accepts three parameters: the first is the character to be removed, the second is a blank string, and the third is the input string.

4. Using Filter Function

You can also use the Filter[1] Function in Python to remove the characters from a string. This method works similarly to the Join method discussed above.

Make a list out of the elements of iterable that function returns true for. An iterator, a sequence, or a container that supports iteration can all be considered iterables, depending on the context.

Results are always of the same type as iterable, except when it is a list and iterable is a tuple. It is assumed that function is None, in which case all elements of iterable that are false are removed.

Let us see in the below code example the usage of str.translate function in Python to eliminate the characters from a string.

#Initializing a String Element
newString = "xCoduberx Visitedx and xTested"

#Using Join and Filter Method to remove the 
#Character from A String
newStr = "".join(filter(lambda newChar: newChar != "x", newString))

#printing the replaced string
print(newStr)

Output:

Coduber Visited and Tested

As you can see in the above code, I was able to filter out the character x from the given string and get the output with any occurrences of the x in the new string variable.

Wrap Up

I hope you were able to get the solution to the problem of how to remove all the occurrences of a character from a string in Python.

I have listed out four methods that you can use. But in terms of efficiency, you should use the first method i.e. replace() to remove any character from a string. It is the most efficient and fastest method of all the methods discussed above.

I haven’t added the str.translate() function as this method does not work in the latest Python version and can only be used in the previous version of the Python.

Let me know in the comment section if you know a better method than the one is discussed above I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Convert GIF To Lottie JSON
  2. How To Get Every nth Element From A List in Python
  3. How To Run Python Script Constantly In Background
  4. How To Convert Python String To Array
  5. Code Example: How To Check If Set Is Empty In Python
  6. Code Examples – Write A List To CSV Using Python
  7. Code Example: Loop Back To The Beginning Of A Program

Leave a Comment