In this article, you will learn to remove specific characters from a string.
Table Of Contents
Let’s discuss some approaches to remove specific characters from a string.
Remove specific characters from a string using replace()
The replace() of string class can be used to replace a character from a string with other characters. So It is possible to delete a character from string by replacing that character with empty string i.e. ”. In this way, we can delete a particular character.
Syntax:
input_str.replace(ch, "")
Where,
- input_str is the input string.
Parameters:
Frequently Asked:
- ch is the character that needs to be deleted from the string.
- An empty string, that will be used as replacement.
It will return a copy of input_str string after removing all the occurrences of specified character from it.
Example:
In this example, we will remove
- All occurrences of character – c
- All occurrences of character W.
input_str="Welcome to thispointer" # Display the actual string print(input_str) listOfChars = ['c', 'W'] # Delete character 'c' and 'W' from input_str for ch in listOfChars: input_str = input_str.replace(ch, "") print(input_str)
Output:
Welcome to thispointer elome to thispointer
We can see that charActer – “c” and “W” are deleted from the string.
Remove specific characters from a string using translate()
We can delete a particular character from a string using the translate() method. It takes a dictionary as argument. In that dictionary we will map the ascii value of character to be deleted with None. It will delete all the occurrences of that character from the string.
Syntax:
input_str.translate({ord(character): None})
Where input_str is the input string.
Example:
In this example, we will delete
- charcater-e
- charcater-r
- charcater-i
- charcater-t
- charcater-p
# Consider the below string input_str="Welcome to thispointer" # Delete character-e from input_str print("After deleting character-e: ",input_str.translate({ord('e'): None})) # Delete character-r from input_str print("After deleting character-r: ",input_str.translate({ord('r'): None})) # Delete character-i from input_str print("After deleting character-i: ",input_str.translate({ord('i'): None})) # Delete character-t from input_str print("After deleting character-t: ",input_str.translate({ord('t'): None})) # Delete character-p from input_str print("After deleting character-p: ",input_str.translate({ord('p'): None}))
Output:
After deleting character-e: Wlcom to thispointr After deleting character-r: Welcome to thispointe After deleting character-i: Welcome to thsponter After deleting character-t: Welcome o hispoiner After deleting character-p: Welcome to thisointer
We can see that specified characters are deleted.
Remove all characters except alphabets using isalpha.
In this case, we can remove all the characters except alphabets from a string using isalpha(). It accepts a character as argument and returns True only if the given character is an alphabet. We will call filter() method with this function as the first parameter and a string as the second parameter. Then filter() function will iterate over all the characters in string and for each character it will call isalpha() to check if it is an alphabet or not. If not, then it will remove that character. In the end it returns a sequence of characters that contains only alphabets. After that, we have to apply join() to join the characters.
Example:
Let’s remove all the non-alphabetic characters from the string.
input_str="Welcome56&%^ to this-(0)pointer" # Display the actual string print(input_str) # Remove all charcaters except alphabets using isalpha with filter() removed= filter(str.isalpha,input_str) finalStr="".join(removed) # Dispay the final string print (finalStr)
Output:
Welcome56&%^ to this-(0)pointer Welcometothispointer
We can see that non-alphabetic characters are removed from the string.
Remove all characters except numbers using isdecimal.
In this case, we can remove all the characters except numbers from a string using isdecimal(). It accepts a character as argument and returns True only if the given character is a number. We will call filter() method with this function as the first parameter and a string as the second parameter. Then filter() function will iterate over all the characters in string and for each character it will call isdecimal() to check if it is a number or not. If not, then it will remove that character. In the end it returns a sequence of characters that contains only numbers. After that, we have to apply join() to join the characters.
Example:
Let’s remove all the non-numeric characters from the string.
input_str="Welcome56&%^ to this-(0)pointer" # Display the actual string print(input_str) # Remove all charcaters except numbers using isdecimal with filter() removed=filter(str.isdecimal,input_str) finalStr = "".join(removed) # Dispay the final string print (finalStr)
Output:
Welcome56&%^ to this-(0)pointer 560
We can see that all non-numeric characters are removed from the string.
Remove all characters except alphabets and numbers from a string
In this case, we can remove all the characters except alphabets and numbers from a string using isalnum(). It accepts a character as argument and returns True only if the given character is a number or an alphabet. We will call filter() method with this function as the first parameter and a string as the second parameter. Then filter() function will iterate over all the characters in string and for each character it will call isalnum() to check if it is a number or an alphabet. If not, then it will remove that character. In the end it returns a sequence of characters that contains only numbers and alphabets. After that, we have to apply join() to join the characters.
Example:
Let’s remove all the non-numeric and non-alphabetic characters from the string.
input_str="Welcome56&%^ to this-(0)pointer" # Display the actual string print(input_str) # Remove all charcaters except numbers and alphanets using isalnum with filter() removed=filter(str.isalnum,input_str) finalStr="".join(removed) # Dispay the final string print (finalStr)
Output:
Welcome56&%^ to this-(0)pointer Welcome56tothis0pointer
We can see that only numeric and alphabetic characters are returned from the string.
Summary
In this article, we learned about different ways to remove specific characters from a string in Python. Happy Coding!!!.