Introduction
This article describes two common methods that you can use to remove characters from a string using Python:
- the
String replace()
method - the
String translate()
method
To learn some different ways to remove spaces from a string in Python, refer to Remove Spaces from a String in Python.
A Python String object is immutable, so you can’t change its value. Any method that manipulates a string value returns a new String object.
The examples in this tutorial use the Python interactive console in the command line to demonstrate different methods that remove characters.
Deploy your Python applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.
Remove Characters From a String Using the replace()
Method
The String replace() method replaces a character with a new character. You can remove a character from a string by providing the character(s) to replace 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:
Outputbc12321cb
The output shows that both occurrences of the character a
were removed from the string.
Remove Newline Characters From a String Using the replace()
Method
Declare a string variable with some newline characters:
- s = 'ab\ncd\nef'
Replace the newline character with an empty string:
- print(s.replace('\n', ''))
The output is:
Outputabcdef
The output shows that both newline characters (\n
) were removed from the string.
Remove a Substring from a String Using the replace()
Method
The replace()
method takes strings as arguments, so you can also replace a word in string.
Declare the string variable:
- s = 'Helloabc'
Replace a word with an empty string:
- print(s.replace('Hello', ''))
The output is:
Outputabc
The output shows that the string Hello
was removed from the input string.
Remove Characters a Specific Number of Times Using the replace()
Method
You can pass a third argument in the replace()
method to specify the number of replacements to perform in the string before stopping. For example, if you specify 2
as the third argument, then only the first 2 occurrences of the given characters are replaced.
Declare the string variable:
- s = 'abababab'
Replace the first two occurrences of the character with the new character:
- print(s.replace('a', 'A', 2)) # perform replacement twice
The output is:
OutputAbAbabab
The output shows that the first two occurrences of the a
character were replaced by the A
character. Since the replacement was done only twice, the other occurrences of a
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 the 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:
Outputac12321ca
The output shows that both occurrences of the b
character were removed from the string as defined in the custom dictionary.
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'}
, that replaces all occurrences of a
, b
, and c
in the given string with None
.
Declare the string variable:
- s = 'abc12321cba'
Replace all the characters abc
with None
:
- print(s.translate({ord(i): None for i in 'abc'}))
The output is:
Output12321
The output shows that all occurrences of a
, b
, and c
were 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}
, that replaces all occurrences of \n
in the given string with None
.
Declare the string variable:
- s = 'ab\ncd\nef'
Replace all the \n
characters with None
:
- print(s.translate({ord('\n'): None}))
The output is:
Outputabcdef
The output shows that all occurrences of the newline character \n
were removed from the string as defined in the custom dictionary.
Conclusion
In this tutorial, you learned some of the methods you can use to remove characters from strings in Python. Continue your learning about Python strings.
Source:
https://www.digitalocean.com/community/tutorials/python-remove-character-from-string