Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 17, 2021 11:32 am GMT

How to remove a character from a string in python?

In this short tutorial, we look at why you would need to remove character from 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
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)

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 characters3 =string.replace("!","")print(s3)//Output - "flexiple"

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)

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"

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 followed.

Code to remove character from string using translate():

s = 'flexiple!'s1 = s.translate({ord('!'): None})print(s1)#Output - "flexiple"

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 :)


Original Link: https://dev.to/hrishikesh1990/how-to-remove-a-character-from-a-string-in-python-4691

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To