remove a character from string python

How remove a character from a string in Python?


A string in Python is a sequence of characters. Sometimes a string may contain unwanted characters. These could be malicious symbols attached to the string while converting from one format to another. For example, if a file is copied from Windows to Linux it may have escape characters.

 Removing characters is a trivial requirement for any application. This may involve removing all occurrences of a character, few occurrences of characters, replace characters from another, etc.    

In this tutorial, we will discuss multiple ways to remove a single character and multiple characters from the Python string.

How to Remove all occurrences of a character with a user-defined function?

This is the very basic method, where read the string character one by one and appended in a new string. If the read character is malicious do not append it to the new string.

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 String class:

The string class has a function name replace(). It takes two arguments, the first argument is which character needs to replace and the second argument is from which character. To remove characters, we can pass an empty character in the second argument. Following is an example for 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 string class.

The join method takes the sequence as input and built a string by appending all members of the sequence. 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.