Python – Remove all occurrences of specific character from String

Remove All Occurrences of Specific Character from String

To remove all the occurrences of a given character from a string in Python, use String.replace() method. Call replace() method on the given string, and pass the search character as first argument, and empty string as second argument to the replace() method.


The syntax to remove all the occurrences of character search in the string x is

x.replace(search, '')


In the following program, we will take a string in name variable, and remove all the occurrences of the search character 'a'.

Python Program

x= 'banana'
search = 'a'

output = x.replace(search, '')
print(f'Input string  : {x}')
print(f'Output string : {output}')
Run Code Copy


Input string  : banana
Output string : bnn


In this tutorial of Python Examples, we learned how to remove all the occurrences of a character from a string, with examples.

Related Tutorials

Code copied to clipboard successfully 👍