This article describes two common methods that you can use to remove characters from a string using Python:
String replace()
methodString translate()
methodTo 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.
replace()
MethodThe 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.
replace()
MethodDeclare 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.
replace()
MethodThe 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.
replace()
MethodYou 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.
translate()
MethodThe 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.
translate()
methodYou 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.
translate()
MethodYou 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.
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Why do you copy standard library’s doc? What’s the point? You won’t teach anybody that way. One can read the documentation 10 times, learn everything about oop, functions, types, loops, etc and won’t be able to write two useful lines of code. Do you know why?
- MIllena
Can you please add the third argument replace() can take, which is the number of times the character will be replaced if there are multiple instances of the character within the string? I’m a beginner in python and I was trying to remove a character from a string but only a certain amount of times, not all instances. When I searched google, your article was the top result. But I had to browse several stack overflow threads to get the information. If you add it to your article, you might just make it easy for the next beginner in python.
- Joy
i want to remove only first char from a string but in this its remove all char related to that… for an example “helloworld” this is string i want remove only first “h” from string “helloworld”
- Prakash choudhary
Nice article.Thanks!
- Dmitriy
Pankaj , your article was nice, but i stuck in solving same kind of problem. Can you help me. I have a dataset and i want to remove certain character from column. Column datatype is object. I am giving you example below- Mileage 21.6 km/kg 18.2 kmpl and so on, I have number of values. I want to remove km/kg and kmpl from the columns values. How can i do that. Thanks & Regards Ajay
- AJAY
Sir ,it removes all the character. For eg: If I remove ‘i’ in ‘initial’. It’s output is ‘ntal’.(it removes all ‘i’ in the string
- Shankar
i want to replace lowercase characters before and after key in given string “there is A key to Success”
- vrushali ingulkar
Hi, I have list of tuple. From that I want to remove few characters from tuple. Please check the below example: x = [(‘url/user/123’, ‘url/site/2’), (‘url/user/125’, ‘url/site/5’)] expected result: [(‘123’, ‘2’), [(‘125’, ‘5’)]]
- Ash
HI @Pankaj i want to replace or remove some variable from string that should not be print in after execution it should remove couple multi variable not only one variable so how can i do that Ex:-- from a input string we need to remove set of variable and need to print after removing it. so how can i do it i have tried with replace and TRAN but its not working. so help me out.
- ibrahim
s=input(); n=int(input()); fs=fs.replace( …); print(fs)
- boopathi