Skip to content
Home » How to Remove Character From String Python

How to Remove Character From String Python

In this tutorial we will discuss about How to Remove Character From String Python.

Replace() or translate can be used to remove a character from a Python string (). Both of these techniques use a provided value to replace a character or string. If an empty string is supplied, the selected character or string is deleted from the string without being replaced.

Python Remove a Character from a String

In this tutorial, we’ll go through how to remove a character or series of characters from a string. A user may enter a symbol that you do not wish to appear in an input field. You could want to delete any occurrences of a specific letter from a string. We’ll use some examples to help us along the way.

We’ll talk about the following approaches:

  • Remove a Character from a String using replace() method
  • Remove a Character from a String using convert() method

Let’s have a look at them in detail.

Remove Character from a String Python using replace() method

The replace() method replaces a new character for the existing one. We can use the replace() method to remove a character from a string by sending it an empty string as a parameter. The string replace() function swaps out a character for a new one. This function replaces any character with a blank string.

Strings are immutable in Python, therefore the replace() function will return a new string while leaving the previous string unchanged.

If you wish to remove the first occurrence of a character from a string, use the replace method with a count argument of 1, as illustrated below.

Example

text= 'Similar Geeks'
print(text.replace(' ','',1))

In the above code we are replacing the space with null character ( empty strings ). That is we are removing the space in the given text.

Output:

SimilarGeeks

If you wish to remove all the occurrences of a specific character from a string, the use the code as illustrated below.

text= 'Similar Geeks'
print(text.replace('s',''))

The above code removes the character s from the given string.

Output

imilar Geek

Remove Character from String Python using translate() Method

The translate() function in Python replaces characters in a string based on the contents of a character table. This method takes a single argument: the translation table containing the characters to map.

To employ this strategy, we must first develop a translation table. This table indicates which characters in a string should be replaced.

Example : To remove all underscores from a username, let’s use the translate() method:

name = 'similar_geeks'
modified_name = name.translate({ ord("_"): None })

print("Your name is: " + modified_name)

Output:

similar geeks

This code substitutes all instances of the “” character with the value None. The Unicode code associated with the “” character is returned by the Python ord() function. The translate() method uses this to determine the character to be removed.

Conclusion

You can use replace() or translate to remove a character or many characters from a string (). Both the replace() and translate() methods produce the same result: a string devoid of the characters you requested.