Python Remove Character From String: In this article, we have discussed how can you remove the character from string in python.

Let’s discuss how to remove a specific character or unwanted character from string in this post.

The following are the different string methods to remove the character from a string element using python programming.

As String is immutable, the methods used below will return a new string and the original string remains unchanged.

In other words, we are just doing string manipulation to achieve the result.

Python Remove Character From String

In this article of python remove character, we have discussed all the steps and methods how can you remove it in python.

Read this article fully, and if you have any doubts dont hesitate to ask us on the comments section.

You can also rate us on the bottom-left section of the vote.

Using the translate method

Python translate() method replaces each character in the given string inline to the provided translation table.

for this, we have to provide the Unicode for the character and None as replacement value to remove the value from the final string.

ord() function used to retrieve the Unicode of a character.

Let’s write a Python program to remove a character in the string using translate function.

inputValue = 'xyz987zyx'
result = inputValue.translate({ord(i): None for i in 'xyz'});


Using the replace method

Python replaces() method replaces character with a given replacement character.

so we can provide the replacement character as empty “” or whitespace, then the character will be removed.

Let’s write a Python program to remove the first character in the original string using replace() function.

inputValue = 'Tracedynamics'
result = inputValue.replace('T', '');


As per the output above, we can see in the new string the character ‘T’ is removed.

similarly, if you want to remove the last character, then substitute the replace method with ‘s’ character.

You May Like,

Remove string number of times

replace() method accepts a third parameter or argument.

This parameter can be no of times a specified string can be replaced.

In the below program, let provide an empty string to remove the specified characters using replace() function.

inputValue = 'Tracedynamics'
result = inputValue.replace('a', '', 2);


Remove newline from string

We can remove a newline from python string using replace() method in the below example.

if there is any whitespace character, we can also remove using replace() method.

inputValue = 'Trace\ndyna\nmics'
result = inputValue.replace('\n', '');


Removing substring from a string

We can remove a certain part of a string(substr) using replace() method.

inputValue = 'Tracedynamics'
result = inputValue.replace('dynamics', '');


You May Also Love,

Remove space from a string

Let’s remove space from a string using replace() method in the below example.

inputValue = 'T r a c e'
result = inputValue.replace(' ', '');


for removing any special characters, we can use the regex pattern(regular expression) in the replace method.

To conclude this tutorial, we covered various types of implementation to remove a character from string using Python Programming.

Pin It on Pinterest

Share This