Introduction
This article describes two common methods you can use to remove characters from a string using Python:
- Method
String replace() - Method
String translate()
To learn a few different ways to remove spaces from a string in Python, see Removing Spaces from a String in Python.
A Python string object is immutable, so you cannot change its value. Any method that manipulates a string value returns a new String object.
The examples in this tutorial use the interactive Python console on the command line to demonstrate different ways to remove characters.
Removing characters from a string using the replace() method
The String() method replaces a character with a new character. You can remove a character from a string by providing the character(s) as the first argument and an empty string as the second argument.
Declare the string variable:
s = 'abc12321cba''Replace the character with an empty string:
print(s.replace('a', ''))The output is:
Output bc12321cbThe output shows that both occurrences of the character a have been removed from the string.
Removing Newline Characters from a String Using the replace() Method
Declare a string variable with multiple newline characters:
s = 'ab\ncd\nef' code... */Replace the newline character with an empty string:
print(s.replace('\n', ''))The output is:
Output abcdefThe output shows that both newline characters (\n) have been removed from the string.
Remove a substring from a string using the replace() method
The replace() method takes strings as arguments, so you can replace a word in a string.
Declare the string variable:
s = 'Helloabc''Replace a word with an empty string:
print(s.replace('Hello', ''))The output is:
The output shows that the string Hello has been removed from the input string.
Remove characters a specified number of times using the replace() method
You can pass a third argument to the replace() method to specify the number of replacements to perform on the string before stopping. For example, if you specify 2 as the third argument, then only the first 2 occurrences of the given characters will be replaced.
Declare the string variable:
s = 'abababab''Replace the first two characters with the new character:
print(s.replace('a', 'A', 2)) # perform replacement twiceThe output is:
Output AbAbababThe output shows that the first two occurrences of the character a have been replaced with the character A. Since the replacement was done only twice, the other occurrences of one remain in the string.
Remove characters from a string using the translate() method
The Python string translate() method replaces each character in the string using a given mapping table or dictionary.
Declare a string variable:
s = 'abc12321cba''Get the Unicode code point value of a character and replace it with None:
print(s.translate({ord('b'): None}))The output is:
Output ac12321caThe output shows that both occurrences of the character b as defined in the custom dictionary have been removed from the string.
Remove multiple characters from a string using the translate() method
You can replace multiple characters in a string using the translate() method. The following example uses a custom dictionary, {ord(i): None for i in 'abc'}, which replaces all occurrences of a, b, and c in the given string with None.
Declare the string variable:
s = 'abc12321cba''Replace all characters abc with None:
print(s.translate({ord(i): None for i in 'abc'}))The output is:
Output 12321The output shows that all occurrences of a, b, and c have been removed from the string as defined in the custom dictionary.
Remove Newline Characters from a String Using the translate() Method
You can replace newline characters in a string using the translate() method. The following example uses a custom dictionary, {ord('\n'): None}, which replaces all occurrences of \n in the given string with None.
Declare the string variable:
s = 'ab\ncd\nef''Replace all \n characters with None:
print(s.translate({ord('\n'): None}))The output is:
Output abcdefThe output shows that all occurrences of the newline character \n have been removed from the string as defined in the custom dictionary.
Result
In this tutorial, you learned the methods you can use to remove characters from strings in Python. Continue your learning about Python strings.









