We use strings in Python to manipulate text data. While analyzing text data we might need to remove some characters from our data. In this article, we will discuss different ways to remove a character from a string in Python.
- Remove a Character From a String in Python Using For Loop
- Using List Comprehension to Remove a Character From a String in Python
- Remove a Character From a String in Python Using the split() Method
- Using the filter() Function to Remove a Character From a String in Python
- Remove a Character From a String in Python Using the replace() Method
- Using the translate() Method to Remove a Character From a String in Python
- Remove a Character From a String in Python Using Regular Expressions
- Conclusion
Remove a Character From a String in Python Using For Loop
We use a for loop to iterate through the elements of an iterable object. We will use the following approach to remove a character from a string using the for loop in Python.
- First, we will define an empty string named
newStr
to store the output string. - Now, we will iterate through the characters of the input string using a for loop.
- During iteration, if we find a character that is not equal to the character we want to remove, we will append the character to
newStr
. - If we find the character that needs to be removed, we skip it.
After execution of the for loop, we will get the output string in the variable newStr
. You can observe this in the following example.
input_string = "Adcictcya"
char_to_remove = "c"
newStr = ""
for character in input_string:
if character != char_to_remove:
newStr += character
print("The input string is:", input_string)
print("The character to delete is:", char_to_remove)
print("The output string is:", newStr)
Output:
The input string is: Adcictcya
The character to delete is: c
The output string is: Aditya
Here, you can see that we have removed the character ‘c
‘ from the input string ‘Adcictcya
‘ to produce the output string ‘Aditya
‘.
Using List Comprehension to Remove a Character From a String in Python
Instead of using a for loop, we can use list comprehension to remove a character from an input string in Python. For this, we will use the following approach.
- First, we will use list comprehension to create a list of characters of the input string after excluding the character that we want to remove. We will store the list in the variable
myList
. - After creating
myList
, we will define an empty string namednewStr
to store the output string. - Now, we will iterate through the elements in the list using a for loop.
- During iteration, we will concatenate each element of
myList
tonewStr
using string concatenation. - After execution of the for loop, we will get the required output string in the variable
newStr
. You can observe this in the following example.
input_string = "Adcictcya"
char_to_remove = "c"
myList = [character for character in input_string if character != char_to_remove]
newStr = ""
for character in myList:
newStr += character
print("The input string is:", input_string)
print("The character to delete is:", char_to_remove)
print("The output string is:", newStr)
Output:
The input string is: Adcictcya
The character to delete is: c
The output string is: Aditya
After creating myList
, we can use the join()
method instead of the for loop to create the output string.
The join()
method, when invoked on a separator string, takes an iterable object as its input argument. After execution, it returns a string in which all the elements of the iterable object are separated by the separator.
We will use the following approach to remove a character from a string in python using the list comprehension and the join()
method.
- First, we will use list comprehension to create a list of characters of the input string after excluding the character that we want to remove. We will store the list in the variable
myList
. - Now, we will define an empty string as a separator.
- After defining the separator, we will invoke the
join()
method on the separator. Here, we will passmyList
as the input argument to thejoin()
method. - After execution, the
join()
method will return the desired string.
You can observe this in the following example.
input_string = "Adcictcya"
char_to_remove = "c"
myList = [character for character in input_string if character != char_to_remove]
separator = ""
newStr = separator.join(myList)
print("The input string is:", input_string)
print("The character to delete is:", char_to_remove)
print("The output string is:", newStr)
Output:
The input string is: Adcictcya
The character to delete is: c
The output string is: Aditya
Remove a Character From a String in Python Using the split() Method
The split()
method is used to split a string into substrings. When invoked on a string, it takes a character as its input argument. After execution, it returns a list of substrings of the original string split at the given character.
To remove a character from a string in Python using the split()
method, we will use the following steps.
- First, we will invoke the
split()
method on the input string. Here, we will pass the character that needs to be removed from the string as an input argument. - After execution, the
split()
method will return a list. We will store the list inmyList
. - Now, we will define an empty string named
newStr
to store the output string. - After this, we will iterate through the elements in the list using a for loop.
- During iteration, we will concatenate each element of
myList
tonewStr
using string concatenation.
After execution of the for loop, we will get the required output string in the variable myStr
. You can observe this in the following example.
input_string = "Adcictcya"
char_to_remove = "c"
myList = input_string.split(char_to_remove)
newStr = ""
for element in myList:
newStr += element
print("The input string is:", input_string)
print("The character to delete is:", char_to_remove)
print("The output string is:", newStr)
Output:
The input string is: Adcictcya
The character to delete is: c
The output string is: Aditya
Instead of using the for loop and string concatenation to create the output string from myList
, we can use the join()
method as follows.
- First, we will invoke the
split()
method on the input string. Here, we will pass the character that needs to be removed from the string as an input argument. - After execution, the
split()
method will return a list. We will store the list inmyList
. - Now, we will define an empty string as a separator.
- After defining the separator, we will invoke the
join()
method on the separator. Here, we will passmyList
as the input argument to thejoin()
method. - After execution, the
join()
method will return the output string.
You can observe the entire process in the following example.
input_string = "Adcictcya"
char_to_remove = "c"
myList = input_string.split(char_to_remove)
separator = ""
newStr = separator.join(myList)
print("The input string is:", input_string)
print("The character to delete is:", char_to_remove)
print("The output string is:", newStr)
Output:
The input string is: Adcictcya
The character to delete is: c
The output string is: Aditya
Using the filter() Function to Remove a Character From a String in Python
The filter()
function is used to filter the elements of an iterable object based on a condition. The filter()
function takes another function as its first input argument and an iterable object as its second input argument. The function given as the first input argument must take the elements of the iterable object as its input and return True or False for each element.
After execution, the filter()
function returns an iterator containing all the elements of the iterable object given in the input argument for which the function given in the first input argument returns True.
To remove a character from a given string using the filter function, we will use the following steps.
- First, we will define a function
myFun
that takes the character of the given string as its input argument. If the character given in the input argument is the character that needs to be deleted, it returns False. Otherwise, it returns True. - After defining
myFun
, we will passmyFun
and the input string as first and second input arguments respectively to thefilter()
function. - After execution, the filter function returns an iterator containing the characters of the string. We will convert the iterator into a list using the
list()
function. We will store the list inmyList
. - Now, we will define an empty string named
newStr
to store the output string. - After this, we will iterate through the elements in the list using a for loop.
- During iteration, we will concatenate each element of
myList
tonewStr
using string concatenation.
After execution of the for loop, we will get the required output string in the variable newStr
. You can observe this in the following example.
def myFun(character):
char_to_remove = "c"
return character != char_to_remove
input_string = "Adcictcya"
char_to_remove = "c"
filter_object = filter(myFun, input_string)
myList = list(filter_object)
newStr = ""
for element in myList:
newStr += element
print("The input string is:", input_string)
print("The character to delete is:", char_to_remove)
print("The output string is:", newStr)
Output:
The input string is: Adcictcya
The character to delete is: c
The output string is: Aditya
You can also use the join() method to create the output string using the following steps.
- First, we will define a function myFun that takes the character of the given string as its input argument. If the character given in the input argument is the character that needs to be deleted, it returns False. Otherwise, it returns True.
- After defining
myFun
, we will passmyFun
and the input string as first and second input arguments respectively to thefilter()
function. - After execution, the filter function returns an iterator containing the characters of the string. We will convert the iterator into a list using the
list()
function. We will store the list inmyList
. - Now, we will define an empty string as a separator.
- After defining the separator, we will invoke the
join()
method on the separator. Here, we will passmyList
as the input argument to thejoin()
method. - After execution, the
join()
method will return the output string.
You can observe the entire process in the following example.
def myFun(character):
char_to_remove = "c"
return character != char_to_remove
input_string = "Adcictcya"
char_to_remove = "c"
filter_object = filter(myFun, input_string)
myList = list(filter_object)
separator = ""
newStr = separator.join(myList)
print("The input string is:", input_string)
print("The character to delete is:", char_to_remove)
print("The output string is:", newStr)
Output:
The input string is: Adcictcya
The character to delete is: c
The output string is: Aditya
Instead of using myFun
, you can use a lambda expression with the filter()
function as shown below.
input_string = "Adcictcya"
char_to_remove = "c"
filter_object = filter(lambda character:character!=char_to_remove, input_string)
myList = list(filter_object)
separator = ""
newStr = separator.join(myList)
print("The input string is:", input_string)
print("The character to delete is:", char_to_remove)
print("The output string is:", newStr)
Output:
The input string is: Adcictcya
The character to delete is: c
The output string is: Aditya
Here, the lambda expression does the same task as myFun
did in the previous code.
Remove a Character From a String in Python Using the replace() Method
The replace()
method is used to replace one character with another in a string. You can also remove a character from a string using the replace()
method. When invoked on a string, the replace()
method takes the character that needs to be replaced as its first argument and the new character as its second input argument. After execution, it returns the modified string.
To remove a character from a string using the replace()
method, we will invoke the replace()
method on the original string. Here, will pass the character that needs to be removed as the first input argument and an empty string as the second input argument to the replace()
method.
After execution, the replace()
method will return the output string. You can observe this in the following example.
input_string = "Adcictcya"
char_to_remove = "c"
newStr = input_string.replace(char_to_remove, "")
print("The input string is:", input_string)
print("The character to delete is:", char_to_remove)
print("The output string is:", newStr)
Output:
The input string is: Adcictcya
The character to delete is: c
The output string is: Aditya
Using the translate() Method to Remove a Character From a String in Python
The translate()
method is also used to replace characters in a string. The translate()
method, when invoked on a string, takes a translation table as its input argument. After execution, it returns the modified string.
To remove a character from a given string, we will first make a translation table. For this, we can use the maketrans()
method.
The maketrans()
method, when invoked on a string, takes a string of characters as its first argument and another string containing the same number of characters as its first argument in its second input argument. After execution, it returns a translation table.
- To make the translation table for removing a character from the given string, we will pass the character that needs to be deleted as the first input argument and a space character as the second input argument to the
maketrans()
method. After execution, themaketrans()
method will return a translation table that maps the character that needs to be deleted to a whitespace character. - Now, we will invoke the
translate()
method on the input string with the translation table as its input argument. After executing thetranslate()
method, we will get a string where the characters that needed to be deleted are replaced by a whitespace character. - Next, we will use the
split()
method to split the string returned by thetranslate()
method into a list. We will store the list in a variable namedmyList
. - Next, we will define an empty string named
newStr
to store the output string. - After this, we will iterate through the elements in the list using a for loop.
- During iteration, we will concatenate each element of
myList
tonewStr
using string concatenation.
After execution of the for loop, we will get the required output string in the variable newStr
. You can observe this in the following example.
input_string = "Adcictcya"
char_to_remove = "c"
translation_table = input_string.maketrans(char_to_remove, " ")
tempStr = input_string.translate(translation_table)
myList = tempStr.split()
newStr = ""
for element in myList:
newStr += element
print("The input string is:", input_string)
print("The character to delete is:", char_to_remove)
print("The output string is:", newStr)
Output:
The input string is: Adcictcya
The character to delete is: c
The output string is: Aditya
Instead of using a for loop and string concatenation, you can use the join() method to create the output string from myList
. For this, you can use the following steps.
- First, will define an empty string as a separator.
- After defining the separator, we will invoke the
join()
method on the separator. Here, we will passmyList
as the input argument to thejoin()
method. - After execution, the
join()
method will return the output string.
You can observe the entire process in the following example.
input_string = "Adcictcya"
char_to_remove = "c"
translation_table = input_string.maketrans(char_to_remove, " ")
tempStr = input_string.translate(translation_table)
myList = tempStr.split()
separator=""
newStr = separator.join(myList)
print("The input string is:", input_string)
print("The character to delete is:", char_to_remove)
print("The output string is:", newStr)
Output:
The input string is: Adcictcya
The character to delete is: c
The output string is: Aditya
The approach using the translate()
method only works for the strings that do not contain whitespace characters. So, you must not use this approach in case the input string has a whitespace character in it.
Remove a Character From a String in Python Using Regular Expressions
Regular expressions provide us with various functions for string manipulation in Python. You can also remove a character from a string in Python using regular expressions. For this, we will use the sub()
function.
The sub()
function is used to replace characters in a string. It takes three input arguments. The first input argument is the character that needs to be replaced. The second argument is the replacement character. Lastly, the third input argument is the original string. After execution, the sub()
function returns the new string.
To remove a character from a string using the sub()
function, we will use the following step.
- In the first argument to the
sub()
function, we will pass the character that needs to be deleted. - We will pass an empty string as the second input argument and the original string as the third input argument to the
sub()
function. - After execution of the
sub()
function, we will get the desired output string.
You can observe this in the following example.
import re
input_string = "Adcictcya"
char_to_remove = "c"
newStr = re.sub(char_to_remove, "", input_string)
print("The input string is:", input_string)
print("The character to delete is:", char_to_remove)
print("The output string is:", newStr)
Output:
The input string is: Adcictcya
The character to delete is: c
The output string is: Aditya
Conclusion
In this article, we have discussed different ways to remove a character from a string in Python. Out of all these approaches, the approach using the re.sub()
function and the replace()
method are the most efficient. Hence, you should use these two approaches to remove a character from a string in Python.
To learn more about python programming, you can read this article on how to remove all occurrences of a character from a list or string in Python. You might also like this article on regression in machine learning. You might also have a look at this article on data analyst vs data scientist.
Stay tuned for more informative articles. Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.