In this Python tutorial, we will discuss how to remove a specific character from a string in Python in different ways. Python has built-in functions that allow you to remove every special character from a string. The following topics we are going to cover.
- How to remove a specific character from a string in Python by using the isalpha() function
- How to remove a specific character from a string in Python by using the regular expression method
- How to remove a specific character from a string in Python by using the isdecimal() function
- How to remove a specific character from a string in Python by using the translate function
- How to remove a specific character from a string in Python by using the filter() method
- How to remove a specific character from a string in Python by using the join and list comprehension method
Remove a specific character from a string in Python using isalpha() function
- If all of the characters in the string are alphabetic or numeric, the string.isalnum() method returns True; otherwise, it returns False.
- isalnum() returns False if the string contains one or more characters that do not match within any of the following groups.
- Now in this example to remove all the specific characters from a string in Python, we can easily use the isalnum() function.
Syntax:
Let’s have a look at the Syntax and understand how to remove a specific character from a string in Python.
str.isalnum()
Note: This function does not contain any parameter and it will always return either false or true.
Example:
Let’s take an example and check how we can remove a specific character from a string in Python by using the str.isalnum() method.
#Input string
Country_name = "Unit$ed S@tates o!f Amer^ica"
# By using the str.isalpha() function
result="".join(m for m in Country_name if m.isalpha())
# Display the new string
print (result)
In the above example, all of the items from the list are joined to generate a string using the “.join” function. Here we use the input string as a country name “Unit$ed S@tates o!f Amer^ica” and now want to remove all the specific characters which are available in the input string.
For this, we have used the m.isalpha() function and which will remove all the specific characters from the string.
Here is the implementation of the following given code
This is how we can remove a specific character from a string by using the isalpha function.
Read: Multiply in Python with Examples
Remove a specific character from a string using regular expression in Python
- To create a pattern that can be used to search for special characters in a string, regular expressions are utilized. Python requires that the re-package be imported before regular expressions may be used.
- When removing special characters from strings in Python, this property is used. The re-package gives us a wide variety of ways to use with regular expression. Python’s re.sub() method can be used to eliminate special characters from a string.
- When we apply this function, we can replace specific elements and it will replace all the characters that match the regular expression.
Syntax:
Let’s have a look at the Syntax and understand the working of the re.sub() method in Python.
re.sub(pattern, repl, string, count=0, flags=0)
- It consists of a few parameters
- pattern: This parameter defines the string/pattern that we want to be replaced.
- repl: This parameter is used to define the pattern with which the pattern is replaced.
- Count: By default, it takes a 0 value and it defines the number of replacements that should occur.
Example:
Let’s take an example and check how we can remove the special characters from a Python string by using the re.sub() function
import re
# string with special characters
Country_name = "U.S.A, Au@st#ralia, Be%lg*iu!m"
print("Input string with specific characters : ",Country_name)
#By using the re.sub() method
result =re.sub("[@,#,%,!,*]", "", Country_name,count=0)
print("Input string after removing the special characters : ",result)
As a result, we will import the re library first to verify that the function, which is found in the re module, and Then, we produce a string, and the input string is Country_name in which we assign the Country name to it “The U.S.A, Au@st#ralia, Be%lg*iu!m”.
Now we want to remove the special characters from the string for this we will create a list of special characters within the re.sub() function.
The re.sub() command is then executed, with the parameters string and count passed in it. Here, we utilize the expression “count=1” to indicate that character removal.
Here is the implementation of the following given code.
As you can see in the Screenshot we have discussed How to remove a specific character from a string by using the regular expression method.
Read: Unexpected EOF while parsing Python
How to remove a specific character from a string in Python using isdecimal()
- In this section, we will discuss how to remove a specific character from a string by using the isdecimal() function.
- If every character in a string is a decimal character, the isdecimal() method returns True. If not, False is returned. Decimal characters are those that have base 10.
Syntax:
Here is the Syntax of the isdecimal() function in Python
isdecimal()
Note: This function does not contain any parameter and it will always return either false or true.
Example:
Let’s take an example and check how to remove a specific character from a string by using the isdecimal() function.
Source Code:
# input string
new_string ="4@1$#4%5^"
# By using the str.isdecimal() function
new_output="".join(i for i in new_string if i.isdecimal())
# Display the new_string
print ("Special characters removed from strings:",new_output)
In the following given code first, we imported the input string and then used the str.isdecimal() function. It loops through the string, determining whether each character is a number or not, and returns the character if it is.
You can refer to the below Screenshot.
This is How to remove a specific character from a string by using the isdecimal function in Python.
Read: Python invalid literal for int() with base 10
Remove a specific character from a string in Python using translate()
- Here we will understand how to remove a specific character from a string by using the translate function in Python.
- To remove a character from the result string, we must give the character’s Unicode code point and the word “None” as a replacement. To determine a character’s Unicode code point, we can utilize the ord() method.
Syntax:
Here is the Syntax of the string.translate() function in Python.
string.translate(table)
Note: This parameter takes only one parameter and defines a translation table storing the mapping between two characters.
Example:
Let’s take an example and check how to remove the specific character from the string in Python by using the translate() method
import string as st
# Input string
cities_of_USA = 'New@York, Los#Angeles, Cal$ifornia, Hou*ston, San&Jose'
# Creating list of special characters
list_of_specific_char=['@','#','$','*','&']
# Display the input
print("Input string : ",cities_of_USA )
new_trans = {special_char: '' for special_char in st.punctuation}
# By using the maketrans()
result = str.maketrans(new_trans)
new_str = cities_of_USA .translate(result)
print("Specific characters removed from string : ",new_str)
In the above example, we have created an input string named ‘cities_of_USA’ and within this, we assigned the city’s names which are located in the United States of America.
Now we have to remove the special characters from the string and for this, we have used the transaction and punctuation() function.
A string containing all the special characters is called the string.punctuation(). Using this, a dictionary with special characters as keys and empty values as values is generated.
Here is the implementation of the following given code
In this example, we have understood how to remove a specific character from a string by using the translate function.
Read: Python check if the variable is an integer
How to remove a specific character from a string in Python using filter() method
- In this section, we will discuss how to remove a specific character from a string by using the filter() method in Python.
- The str.isalpha method will be applied to every element in the string by the filter() function, and if it returns True, the element will be returned. If not, it will ignore the item.
- The join () function will join all of the elements in the iterator with an empty string after the filter() returns an iterator with all of the alphabets in the string.
Example:
Let’s take an example and check how to remove a specific character from a string by using the filter() method
# Input string
Bikes_in_USA = 'Ap@ril#ia, Ka$was%aki, Du&ca!ti'
# By using the filter and isalpha() method
new_var=filter(str.isalpha,Bikes_in_USA)
result="".join(new_var)
print ("Remove specific characters from string :",result)
In the following given code first we created the input string Bikes_in_USA = ‘Ap@ril#ia, Ka$was%aki, Du&ca!ti’ and then used the filter() function and within this, we passed the str.isalpha as an argument.
You can refer to the below Screenshot
This is how we can remove a specific character from a string by using the filter method in Python.
Read: Check if a number is a prime Python
Remove a specific character from a string in Python using join and list comprehension method
- In this example, we will discuss how to remove a specific character from a string in Python by using the join and list comprehension method in python.
- By using the list comprehension and join method we can easily involve splitting each string element into its associated list element, then joining each of them to declare a new string.
Example:
Let’s take an example and check how to remove a specific character from a string by using the join and list comprehension method in python.
Source Code:
# Input String
Cars_in_USA = "Te@sla, Toyota, Ford"
print("Input string:",Cars_in_USA)
# By using the join() method
output = ''.join([Cars_in_USA [i] for i in range(len(Cars_in_USA )) if i != 2])
# Display the Content
print ("First character remove from string: ",output)
Here is the implementation of the following given code
Also, take a look at some more Python tutorials.
- What does the percent sign mean in python
- How to add zeros before a number in Python
- How to find a string from a list in Python
- Python built-in functions with examples
- Python syntaxerror: ‘return’ outside function
- Python program for binary search
In this article, we have discussed how to remove a specific character from a string in Python in different ways. And also we have covered some different methods that allow you to remove every special character from a string.
- How to remove a specific character from a string in Python by using the isalpha() function
- How to remove a specific character from a string in Python by using the regular expression method
- How to remove a specific character from a string in Python by using the isdecimal() function
- How to remove a specific character from a string in Python by using the translate function
- How to remove a specific character from a string in Python by using the filter() method
- How to remove a specific character from a string in Python by using the join and list comprehension method
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.