How to remove a character from string in Python
In this article, you are going to learn about how to remove a character from a string in Python.
Python is an Object-Oriented and interpreted programming language. In python, there are some fundamental topics, string is one of them. Strings are arrays of bytes that represent Unicode characters in python. You need to work with strings in your day-to-day programming journey. So, having a strong grab on this topic is essential. In this article, we will cover how you can remove a character from a string in Python.
In python, strings are immutable which means you can not change them. So, you need to keep this thing in your mind. There are different ways of removing a character from a string but the easiest and widely used ones are – using replace() and using translate() method. We will explore these two functions first and then also see some other ways of performing this action.
replace() method
The replace() method is the most popular method for removing a character from a string in python. The method expects three parameters. The first one is the character that you want to remove, the second one that you want to replace and the third one is optional that denotes the number of occurrences. Let’s see an example of using it
old_string = "Good Moening!"
new_string = old_string.replace("e","r")
print(new_string)
//Output: Good Morning!
Here, you can see that the first string Good Morning! was misspelled and we can fix them with the replace() method. Let’s see how you can remove a character.
old_string = "Good Morning!"
new_string = old_string.replace("!"," ")
print(new_string)
//Output : Good Morning
Here, you can see that we simply remove the exclamation sign with nothing. This is how the replace() method works.
translate() method
Another way of removing a character from a string is to use the translate() method. It also returns a string after removing the values that are passed in the table. Let’s see an example of it in the below section:
my_string = "Good Molning!"
a = "l"
b = "r"
table = my_string.maketrans(a, b)
print(my_string.translate(table))
//Output : Good Morning!
Here, you can see that the process of doing it is different from the replace() method but we are getting the same result. Let’s remove the exclamation sign from the string.
old_string = "Good Morning!"
new_string = old_string.translate({ord('!'): None})
print(new_string)
//Output: Goog Morning
Here, you can notice the last character of the string has been removed.
Note: You always need to use none while replacing a character using the translate() method.
Besides these methods, you may use the naive method for removing a character from the string in python. Let’s see an example of performing it.
old_string = "Goood Morning!"
new_string = ""
for i in range(len(old_string)):
if i != 2:
new_string = new_string + old_string[i]
print (new_string)
//Output: Good Morning!
Here, in this method, all we need to do is to specify the length of the character that we want to remove. You can notice that our string has an extra o that has been removed.
These are some useful methods for removing a character from the string in Python and you may follow these approaches for performing this action.