How to remove a character from a string in Python?
In Python, a string is a sequence of characters. Sometimes, a string may contain undesirable characters. These could be malicious symbols added to the string during the conversion process. For instance, if you copy a file from Windows to Linux, it may contain escape characters.
For any application, removing characters from a string is a trivial requirement. This may involve deleting all instances of a character, a few instances of a character, or replacing characters with those from another.
This tutorial will discuss multiple ways to remove single or multiple characters from a Python string.
How to Remove all occurrences of a character with a user-defined function?
This is the simplest approach, in which the characters are read one by one and appended to a newly constructed string. Do not append the read character to the new string if it is malicious.
def removeCharacter(in_str, char):
new_str = "";
for i in range(0, len(in_str)):
if in_str[i] != char:
new_str = new_str + in_str[i]
return new_str;
pass;
inputString = "I love to do ;; python programming;.";
print("Original String = ", inputString);
#Removing all occurrence of ";"
print("New string after removal ; is ", removeCharacter(inputString,';'));
Output->
Original String = I love to do ;; python programming;. New string after removal ; is I love to do python programming.
By using replace() method of the String class:
Replace is a function in the string class (). It accepts two arguments, the first is the character to be replaced, and the second argument is a new character. To remove all occurrences of unwanted characters, we can pass an empty character in the second argument.
The following is an example of replace()
function.
inputString = "I love to do ;; python programming;.";
print("Original String = ", inputString);
Removing all occurrence of ";";
print("New string after removal ; is ",inputString.replace(';',''));
Output->
Original String = I love to do ;; python programming;. New string after removal ; is I love to do python programming.
Remove multiple Characters –
def removeCharacter(in_str, charlist):
for i in charlist:
in_str = in_str.replace(i,"");
return in_str;
pass;
inputString = "I love to do ;; python ::programming;.!!!";
print("Original String = ", inputString);
Removing all occurrence of ";"
chars_to_remove = [';', ':','!'];
print("New string after removal ; is ", removeCharacter(inputString,chars_to_remove));
Output->
Original String = I love to do ;; python ::programming;.!!! New string after removal ; is I love to do python programming.
Remove characters using the join method of the string class.
The join method inputs the sequence and builds a string by appending all sequence members. In the following example, the original string first converts into the list of characters without the character to remove. Then by using the join() method, we get the new string with no unwanted characters.
inputString = "I love to do ;; python programming;.";
newString="";
print("Original String = ", inputString);
Removing all occurrence of ";";
list = [inputString[i] for i in range(len(inputString)) if inputString[i] != ';'];
print(" List = ", list);
newString = newString.join(list);
print("New string after removal of character ; is ",newString);
Output->
Original String = I love to do ;; python programming;. List = ['I', ' ', 'l', 'o', 'v', 'e', ' ', 't', 'o', ' ', 'd', 'o', ' ', ' ', 'p', 'y', 't', 'h', 'o', 'n', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g', '.'] New string after removal of character ; is I love to do python programming.