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.
Contents
Python Remove Character From String
remove a character from the string in python you can do by two different methods, one is using translate function and the second one is using replace functions, You can remove by using both translate and replace functions. In this article, we have discussed all the steps and methods of how can you remove it in python. Read this article fully, and if you have any doubts don’t hesitate to ask us in the comments section. You can also rate us on the bottom-left section of the vote.
Python Remove Character from String 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 a translate function.
inputValue = 'xyz987zyx'
result = inputValue.translate({ord(i): None for i in 'xyz'});
print(result);
Output:
987
Python Remove Character from String 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', '');
print(result);
Output:
racedynamics
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,
Python 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);
print(result);
Output:
Trcedynmics
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', '');
print(result);
Output:
Tracedynamics
Removing substring from a string
We can remove a certain part of a string(substr) using replace() method.
inputValue = 'Tracedynamics'
result = inputValue.replace('dynamics', '');
print(result);
Output:
Trace
You May Also Love,
Remove space from a string
Let’s remove space from a python string using replace() method in the below example.
inputValue = 'T r a c e'
result = inputValue.replace(' ', '');
print(result);
Output:
Trace
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..