7 Ways to Remove Character From String Python

While working with strings, there are many times when we have to remove a character from string python. Suppose you want to delete a particular character entirely from the string, or if you want to remove all the special characters from the string. 

Removing Characters from a String can be amazingly useful in many applications. Removing spam, filtering texts, and handling text sentiments require a basic concept of being able to remove a character from a string. As strings are immutable in Python, you need to assign strings to a new variable or reassign a new object to replace it.

Ways to Remove Characters from String in Python 

In this post. we’ll mention 7 unique ways to remove the character from the string. These methods are as follows –

  1. Using loop
  2. Remove characters from string in python using slicing 
  3. Using replace() 
  4. Remove characters from string in python using translate() 
  5. Using join() 
  6. remove characters from string Using regular expression 
  7. remove characters from string Using filter() 

1. Remove characters from string Using a loop

This is a naive method. Here we apply a ‘for’ or ‘while’ loop to go through the string and if condition and the if the condition to check whether a particular character is present or not. We will save the resultant string in a new variable. 

Suppose you want to remove every occurrence of ‘r’ from the string- 

remove characters from string in python
input_string = "remove all r from this string" 
result="" 
for i in input_string: 
   if i=='r': 
       pass 
   else: 
       result+=i 
print("input string : ",input_string) 
print("resultant string : ",result) 
Output- 
input string : remove all r from this string 
resultant string : emove all fom this sting 

Suppose we want to remove all the capital letters

All capital letters can be removed by using a conditional statement shown as below –

string="My name is Ashwini Mandani" 
new_string="" 
for i in string: 
   if i >='A' and i<='Z': 
       pass 
   else: 
        new_string+=i 
print(new_string) 
Output-  
y name is shwini andani 

2. Remove characters from string in python using slicing 

Suppose we want to remove the character at a particular index- 

string="remove character at index=5" 
string=string[:5]+string[6:] 
print(string) 
Output- 
remov character at index=5 
remove characters from string in python

To remove the last character of the string 

string="keep learning" 
print(string[:len(string)-1]) 
Output- 
keep learnin 

To remove the first character- 

string="keep learning" 
print(string[1:]) 
Output- 
eep learning 

3. Remove characters from string Using replace() 

It is a quite common way (though inefficient) remove a character from a string Python. 

str1="Remove using replace functions" 
str1=str1.replace('e',"") 
print(str1) 
Output- 
Rmov using rplac functions 
Remove characters from string in python

You can also remove a list of characters. 

x=['1','2','3','4','5','6','7','8','9','0'] 
str1="ashwini0112mandani" 
for i in x: 
   str1=str1.replace(i,"") 
print(str1) 
Output- 
ashwinimandani 

Suppose we want to remove a character only a certain number of times.

string="ashwini is a python programmer" 
string=string.replace("a","",1) 
print(string)
Output- 
shwini is a python programmer 

4. Remove characters from string in python using translate() 

One of the most efficient ways of removing a character from string in python is by using translate(). To use this method, we first have to use the maketrans() method to create a table. The table here defines which character should be replaced. The maketrans() should contain at least one and at most three arguments. And if we are giving only one argument, then it must be a dictionary. 

remove characters
#List of characters we want to remove 
list1=['+','/','-','*'] 
string="let+ us- use/ tran-slate fu*nction" 
#maketrans(): make a table showing to remove list1 with "" 
translation=string.maketrans({i:"" for i in list1}) 
#applying the table on the string using the translate() 
string=string.translate(translation) 
print(string) 
Output-
let us use translate function

5. Remove characters from string Using join() 

In this method we will use list comprehension and join(). 

string="ashw+ini i-s pyt++hon progr+am-mer" 
list1=['+','-'] 
string="".join(i for i in string if i not in list1) 
print(string) 
Output- 
ashwini is python programmer 

6. Remove characters from string Using regular expression 

We can also remove a character using the sub() function of regular expressions. It is generally used to substitute or replace one character with another, but we can also remove characters using this.  

To use regular expressions we have to import the re library  

import re 
string="Remove all l's" 
string1=re.sub("l", "", string) 
print("Original: ",string) 
print("new: ",string1) 
Output- 
Original: Remove all l's
new: Remove a 's

We can also remove multiple characters using ‘|’.  

import re 
string="remove all the l's and also remove all the r" 
string1=re.sub("l|r", "", string) 
print("Original: ",string) 
print("new: ",string1) 
Output- 
Original: remove all the l's and also remove all the r 
new: emove a the's and aso emove a the 

7. Remove characters from string Using filter() 

We can also refine our string using filter() and lambda keyword. 

string="Removing characters using filter method" 
characters=['c','R','m'] 
new=filter(lambda i: i not in characters,string) 
# filter() returns an iterator 
new_string="" 
for i in new: 
    new_string+=i 
print(new_string) 
Output- 
eoving haraters using filter ethod 

Must Read

Conclusion

Whenever we want to remove a character from string python, we generally use any of the above methods. The most efficient way to remove characters in a string in python is by using the translate().

Try to run the programs on your side and let me know if you have any queries.

Happy Coding!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments