How to remove a specific character from string in Python

In this Python tutorial, we will discuss how to remove a specific character from a string in Python. In addition, Python has built-in functions that allow you to remove every special character from a string, so we will discuss each of the functions in detail.

Special Characters in Python

In Python, special characters include symbols that are not alphanumeric like ‘@’, ‘#’, ‘%’, ‘*’, ‘(‘, ‘)’, etc.

Remove special characters from string in Python

We will delve into different methods of removing special characters from a string in Python with examples.

Method 1: Using the replace() function in Python

The replace() method is a built-in function in Python that replaces a specified phrase with another specified phrase. Here is how you can use it to remove a special character.

Example# Let’s take a string that represents a popular American food item.

string = "Cheese@Burger#"
string = string.replace("@", "")
string = string.replace("#", "")
print(string) 

Output:

How to remove a specific character from string in Python

The drawback of this approach is that you need to know all the special characters that exist in the Python string. Also, if the Python string contains many different special characters, the code can get quite lengthy.

Method 2: Using Python Regular Expressions

Regular expressions, also known as regex, are sequences of characters that form a search pattern. Python’s re module provides support for regular expressions. To remove special characters, we can use the Python re.sub() function.

Example# Let’s use the name of a famous American landmark

import re

string = "Statue@of#Liberty"
string = re.sub('[^A-Za-z0-9]+', '', string)
print(string) 

In the above code, [^A-Za-z0-9] is a pattern that matches any character that is not a letter or a number. Then, Python re.sub() replaces all such characters with an empty string.

Output:

Remove a specific character from string in Python

Method 3: Using Python translate() and maketrans() functions

Python string class provides a method called translate() that can be used with maketrans() function to replace or remove all the specified characters from a string.

Example# Let’s use a phrase that’s synonymous with the United States

string = "Land@of#the#Free"
string = string.translate(string.maketrans("", "", "@#"))
print(string)

The Python maketrans() function returns a translation table that can be used with the translate() method to replace the specified characters.

Output:

Python remove a specific character from string

Method 4: Using List Comprehension in Python for how to remove punctuation from a string python

In Python, list comprehension is a compact way of creating a list from one or more iterables. It can have an optional condition to filter items. Here is how to use it to remove special characters.

Example# Let’s use a string representing a US city and its popular nickname

string = "Chicago@-#The#Windy#City"
string = ''.join(e for e in string if e.isalnum())
print(string) 

The Python isalnum() method checks whether all the characters in a string are alphanumeric (either alphabets or numbers). If true, the character is included in the new string. Otherwise, it is omitted.

Output:

Remove a specific character from string Python

Conclusion

In Python, there are many ways to remove special characters from a string. The best method to use depends on your specific needs.

The replace() method in Python is handy for removing a few known characters. Regular expressions offer powerful and flexible solutions for dealing with patterns.

The translate() and maketrans() methods are useful for handling a larger set of characters, and the Python list comprehension provides a succinct way to filter characters.

Also, take a look at some more Python tutorials.