- How To Remove Characters from a String in Python
- Remove Characters From a String Using the replace() Method
- Remove Newline Characters From a String Using the replace() Method
- Remove a Substring from a String Using the replace() Method
- Remove Characters a Specific Number of Times Using the replace() Method
- Remove Characters From a String Using the translate() Method
- Remove Multiple Characters From a String using the translate() method
- Remove Newline Characters From a String Using the translate() Method
- Conclusion
- How to Remove a Specific Character from a String in Python
- How to Remove a Specific Character from a String in Python Using the replace() Method
- How to Remove Multiple Characters from A String in Python Using the replace() Method
- Remove Multiple Characters With Method Chaining
- Remove Multiple Characters With A for Loop
- Remove Multiple Characters With Regular Expressions
- How to Remove a Specific Character from a String in Python Using the translate() Method
- How to Remove Multiple Characters from a String in Python Using the translate() Method
- Conclusion
How To Remove Characters from a String in Python
This article describes two common methods that you can use to remove characters from a string using Python:
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.
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:
Replace the character with an empty string:
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:
Replace the newline character with an empty string:
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:
Replace a word with an empty string:
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:
Replace the first two occurrences of the character with the new character:
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:
Get the Unicode code point value of a character and replace it with None :
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, , that replaces all occurrences of a , b , and c in the given string with None .
Declare the string variable:
Replace all the characters abc with None :
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, , that replaces all occurrences of \n in the given string with None .
Declare the string variable:
Replace all the \n characters with None :
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.
Want to deploy your application quickly? Try Cloudways, the #1 managed hosting provider for small-to-medium businesses, agencies, and developers — for free. DigitalOcean and Cloudways together will give you a reliable, scalable, and hassle-free managed hosting experience with anytime support that makes all your hosting worries a thing of the past. Start with $100 in free credits!
How to Remove a Specific Character from a String in Python
Dionysia Lemonaki
When coding in Python, there may be times when you need to remove a character from a string.
Removing characters from strings is handy if you are working with user-generated inputs and need to clean your data and remove unwanted characters.
Specifically, you may need to remove only one instance of a character or even all occurrences of a character from a string.
Python offers many ways to help you do this.
Two of the most common ways to remove characters from strings in Python are:
When using either of the two methods, you can specify the character(s) you want to remove from the string.
Both methods replace a character with a value that you specify. And if you specify an empty character instead, the character you want to remove gets deleted without a replacement.
Something to note when using these methods is that the original string doesn’t get altered since strings are immutable. Instead, both methods return a new modified string that doesn’t contain the characters you wanted to remove.
In this article, you will learn how to use both methods to remove a character or multiple characters from a string with the help of coding examples.
Here is what we will cover:
How to Remove a Specific Character from a String in Python Using the replace() Method
The general syntax for the replace() method looks something similar to the following:
string.replace( character, replacement, count)
- You append the replace() method on a string .
- The replace() method accepts three arguments:
- character is a required argument and represents the specific character you want to remove from string .
- replacement is a required argument and represents the new string/character that will take the place of character .
- count is an optional argument that represents the maximum number of character occurrences you want to remove from string . If you don’t include count , then by default, the replace() method will remove all the occurrences of character .
The replace() method doesn’t modify the original string. Instead, its return value is a copy of the original string without the characters you wanted to remove.
Now, let’s see replace() in action!
Say you have the following string, and you want to remove all of the exclamation marks:
my_string = "Hi! I! Love! Python!"
Here is how you would remove all the occurrences of the ! character:
my_string = "Hi! I! Love! Python!" my_new_string = my_string.replace("!", "") print(my_new_string) # output # Hi I Love Python
In the example above, I appended the replace() method to my_string .
I then stored the result in a new variable named my_new_string .
Remember, strings are immutable. The original string remains unchanged — replace() returns a new string and doesn’t modify the original one.
I specified that I wanted to remove the ! character and indicated that I wanted to replace ! with an empty character.
I also didn’t use the count argument, so replace() replaced all occurrences of the ! character with an empty one.
The original string stored in a variable my_string has four occurrences of the ! character.
If I wanted to remove only three occurrences of the character and not all of them, I would use the count argument and pass a value of 3 to specify the number of times I would like to replace the character:
my_string = "Hi! I! love! Python!" my_new_string = my_string.replace("!", "", 3) print(my_new_string) # output # Hi I love Python!
How to Remove Multiple Characters from A String in Python Using the replace() Method
There may be a time when you will need to replace multiple characters from a string.
In the following sections, you will see three ways you can achieve this using the replace() method.
Remove Multiple Characters With Method Chaining
One way you could achieve this is by chaining the replace() method like so:
my_string = "Hi!? I!? love!? Python!?" my_new_string = my_string.replace("!","").replace("?","") print(my_new_string) # output # Hi I love Python
That said, this way of removing characters can be quite difficult to read.
Remove Multiple Characters With A for Loop
Another way to accomplish this is to use the replace() method inside a for loop:
my_string = "Hi!? I!? love!? Python!?" replacements = [('!', ''), ('?', '')] for char, replacement in replacements: if char in my_string: my_string = my_string.replace(char, replacement) print(my_string) # output # Hi I love Python
I first created a string that contains the two characters I want to remove, ! and ? , and stored it in the variable my_string .
I stored the characters I want to replace, along with their replacements, in a list of tuples with the name replacements — I want to replace ! with an empty string and ? with an empty string.
Then, I used a for loop to iterate over the list of tuples (if you need a refresher on for loops, give this article a read).
Inside the for loop, I used the in operator to check whether the characters are inside the string. And if they were, I used the replace() method to replace them.
Finally, I reassigned the variable.
Remove Multiple Characters With Regular Expressions
And yet another way to accomplish this is by using the regular expression library re and the sub method.
You first need to import the library:
Then, specify the group of characters you want to remove (in this case, the ! and ? characters), along with the characters you want to replace them with. In this case, the replacement is an empty character:
import re my_string = "Hi!? I!? love!? Python!?" my_new_string = re.sub('[!?]',"",my_string) print(my_new_string) # output # Hi I love Python
How to Remove a Specific Character from a String in Python Using the translate() Method
Similarly to the replace() method, translate() removes characters from a string.
With that said, the translate() method is a bit more complicated and not the most beginner-friendly.
The replace() method is the most straightforward solution to use when you need to remove characters from a string.
When using the translate() method to replace a character in a string, you need to create a character translation table, where translate() uses the contents of the table to replace the characters.
A translation table is a dictionary of key-value mappings, and each key gets replaced with a value.
You can use the ord() function to get the character’s Unicode value and then map that value to another character.
This method returns a new string where each character from the old string gets mapped to a character from the translation table.
The return value is a new string.
Let’s see an example using the same code from the previous sections:
my_string = "Hi! I! love! Python!" my_new_string = my_string.translate( < ord("!"): None >) print(my_new_string) # output # Hi I love Python
In the example above, I used the ord() function to return the Unicode value associated with the character I wanted to replace, which in this case was ! .
Then, I mapped that Unicode value to None — another word for nothing or empty — which makes sure to remove it. Specifically, it replaced every instance of the ! character with a None value.
How to Remove Multiple Characters from a String in Python Using the translate() Method
What if you need to replace more than one character using the translate() method? For that, you can use an iterator like so:
my_string = "Hi!? I!? love!? Python!?" my_new_string = my_string.translate( < ord(i): None for i in '!?'>) print(my_new_string) # output # Hi I love Python
In the example above, I replaced both ! and ? characters with the value None by using an iterator that looped through the characters I wanted to remove.
The translate() method checks whether each character in my_string is equal to an exclamation point or a question mark. If it is, then it gets replaced with None .
Conclusion
Hopefully, this article helped you understand how to remove characters from a string in Python using the built-in replace() and translate() string methods.
Thank you for reading, and happy coding!