DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Edited on • Originally published at flexiple.com

How to remove a character from a string in python?

In this short tutorial, we look at why you would need to remove a character from a string in Python and we look at the different methods we can use to achieve this.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Content

Why would you need to remove character from string python?

Strings are arguably the commonly used data type in Python and when used to such an extent you come across a plethora of errors. Out of which the most common ones are the new tab escape sequence \n getting appended to the ending of a string or special characters in the place of accent marks. These errors are quite frequent when dealing with files.

No matter what caused the formatting to break, it is essential that we are able to remove these characters from the string. And Python has a few handy inbuilt functions that can be used.

Methods to remove character from string in python

While trying to remove character from string in Python, you should keep in mind that strings are immutable i.e they cannot be changed. So remember to either reassign the updated string to the same variable or store the updated string in a new variable.

With that out of the way let's look at methods we can use to remove character from string in Python.

Using replace():

Replace is one of the most common methods used to remove a character from a string in Python. Although, replace() was intended to replace a new character with an older character - using "" as the new character can be used to remove a character from a string. The syntax of replace() is as follows.

Syntax of replace():

string.replace(old character, new character, count)
Enter fullscreen mode Exit fullscreen mode

string is the positional argument containing the string

Parameters:

old character - The character that needs to be replaced

new character - The character you are looking to replace with

count - This is an optional parameter, and is used to specify how many occurrences of the old character you intend to replace

Code to remove a character from string in python using replace():

s = "flexible!"
s2 = string.replace("b","p")

print(s2)
// Output - "flexiple!"

//Code to remove a character
s3 =string.replace("!","")

print(s3)
//Output - "flexiple"
Enter fullscreen mode Exit fullscreen mode

In case you are looking to remove multiple characters from a string, you can add all the characters to an iterable, and replace each element with a loop. Also, remember that strings are immutable and you need to assign the updated string to a variable.

Using translate():

translate() is another method that can be used to remove a character from a string in Python. translate() returns a string after removing the values passed in the table. Also, remember that to remove a character from a string using translate() you have to replace it with None and not "".

Syntax of translate():

string.translate(table)
Enter fullscreen mode Exit fullscreen mode

Parameters:

table - The values could either be a dictionary or a mapping table that describes what values should be replaced.

Code to use translate():

s = "Flexible"
x = "b"
y = "p"
table = s.maketrans(x, y)
print(s.translate(table))
#Output - "Flexiple"
Enter fullscreen mode Exit fullscreen mode

Although this method can be quite tedious there is a way around it. But before that, you need to familiarize yourself with the below concept.

From Python version 3 and above strings are Unicode, which essentially means strings get converted into a Unicode, this helps maintain a uniform code irrespective of the language the programmer uses. You can read more about this here.

This is why you are required to pass the variables into a table. But by using the ord() function, this function returns the Unicode of a character. This can be used to remove a character from a string. The code is as follows.

Code to remove character from string using translate():

s = 'flexiple!'
s1 = s.translate({ord('!'): None})

print(s1)
#Output - "flexiple"
Enter fullscreen mode Exit fullscreen mode

Remember in order for translate() to remove a character you need to replace it with None

Closing Thoughts

Out of both the methods, I personally find myself using replace() more often than translate() as it is easier to comprehend. Although once you understand how translate() works it again becomes a personal preference.

However, remember that strings are Immutable and you need to store the updated string if you intend to use it further.

Do let me know your thought in the comment section below :)

Top comments (1)

Collapse
 
aliasalan profile image
Alias

This is very interesting. I've always removed strings/updated per array based on first-last used as was needed when used but this is a very interesting take. I like the replace function, it would be very handy in top-end user-driven applications.