How to Remove a Character From a String in Python?

In Python, the “string” is used to store the textual data. It is always enclosed inside single or double quotation marks. Python provides various functions to perform operations on strings, such as removing, adding, appending, etc. To remove a character from a string different methods are used in Python.

In this blog post various methods are explained with multiple examples using the following contents:

Method 1: Using replace() Function

The below code used the “replace()” function to remove the character from the string:

Code:

string_value = "Python Guide"
output = string_value.replace('Guide', '')
print (output)

In the above code:

  • The string named “string_value” is initialized in the program.
  • The “string.replace()” function removes the characters by taking the removed substring as a first argument and the empty string as a second argument.

Output:

The sequence of the character “Guide” has been removed from the given string.

We can also remove specific occurrences of characters from the string using the occurrence parameter. The below code removes the first occurrence of the symbol “-” from the given string:

Code:

string_value = "Python-Java-C++"
output = string_value.replace('-', '', 1)
print (output)

In the above code, the “string.replace()” function accepts the integer value “1” as a third argument and removes the first occurrence of the character “” from the given string.

Output:

The first occurrence of the character “” has been removed from the given string.

Method 2: Using join() and List Comprehension

The “join()” function is used along with the list comprehension to remove a specific character from the string:

Code:

string_value = "Python Developer"
index = 4
output = ''.join([string_value[i] for i in range(len(string_value)) if i != index]) 
print (output)

In the above code:

  • The string and index position value is initialized in the program.
  • The “index” variable takes the index position of the character that needs to be eliminated from the given string.
  • The list comprehension is used along with a for loop to iterate over the string and remove the specific character from the string using the “if” statement.
  • The list comprehension returns the string that appends into the empty string using the “join()” function.

Output:

The character “o” placed at index “4” has been eliminated from the input string.

Method 3: Using for Loop

The traditional “for loop” is used in the below code to remove a specific character from the string:

Code:

string_value = "Python-Guide"
empty_string = ""
index = 6

for item in range(0, len(string_value)):
    if item != index:
        empty_string = empty_string + string_value[item]
 
print (empty_string)

In the above code:

  • The input string, empty string and the index position for the removed character are initialized in the program.
  • The “len()” function is used to calculate the length of the given string named “string_value”. 
  • Secondly, the calculated length value is passed to a “range()” function to generate the specific range.
  • The for loop iterates over the specific range and checks the specified “index” position using the “if” condition.
  • For every index value other than the specified index is added into the empty string using the concatenation method.

Output:

The character “-” has been removed from the given string.

Method 4: Using translate() Method

In the following code, the “translate()” function is used to remove the specific character from the string:

Code:

string_value = 'Linux#Debian#Ubuntu'
value = '#'
print(string_value.translate({ord(i): None for i in value}))

In the above code:

  • The string named “string_value” is initialized in the program.
  • The specific character that needs to be substituted is assigned to the variable “value”.
  • The “string.translate()” function is used to replace the particular character from the given string.

Output:

The character “#” has been removed from the given string.

Method 5: Using String Slicing Method

The below code uses the string slicing technique to remove a character from the string:

Code:

string_value = "Python-Guide"
output = string_value[:6] +  string_value[7:]
print (output)

In the above output:

  • The string named “string_value” is initialized in the program.
  • The “string slicing” method is used along with concatenation to remove the character from the given string.

Output:

The above output verified that the character “-” has been removed.

Conclusion

To remove a character from the string the “replace()” function, “join()” function with list comprehension, “for” loop, “translate()” method, and string slicing method are used in Python. The “join()” function is used along with list comprehension to replace the specific character in the string. The “replace()” function is used to remove/replace the specific occurrence of a character. This post delivered a detailed guide on how to remove a character from a string using 5 different methods.