How to Remove Special Characters from a String in Python?


Strings are one of the most important data types in Python. They are used to store text, which can be made up of letters, numbers, and special characters. Sometimes, you may want to remove certain characters from a string.

For example, you may want to remove all punctuation marks from a string so that only letters and numbers remain.

Or you may want to remove all non-ASCII characters from a string. In this blog post, we’ll show you how to remove special characters from a string in Python.

How to Remove All Non-ASCII Characters from a String in Python

The first method we’ll show you is how to remove All Non-ASCII Characters from a string using the re module’s sub() method.

To remove all All Non-ASCII Characters from a string, we can use the following code:

import re s = "Hello! This is a test." s = re.sub('[^A-Za-z0-9]+',' ', s) #s = re.sub('\W+',' ', s ) print(s) # Hello This is a test
Code language: Python (python)

As you can see, all of the punctuation marks have been removed from the string.

Delete a string by replacing it with an empty string

If you want to get rid of any string in a string, just replace it with an empty string (“”).

s = '123-abc-567-yyy-090-jkl' print(s.replace('abc', '')) # 123--567-yyy-090-jkl
Code language: Python (python)

As you can see, It replaces “abc” with an empty string (”)

Remove Leading and Trailing Characters from Strings in Python using  .strip() method

When the.strip() method has no arguments, it gets rid of any white space at the beginning or end of a string.

By adding characters as arguments, you tell the function which characters you want to remove from the beginning and end of the string.

str_greeting = " Hello! " result = str_greeting.strip() print(result,"How are you?") #output #Hello! How are you? str_greeting = "Hello World?" result = greeting.strip("H?") print(result) #output #ello World
Code language: Python (python)

Remove trailing (right) characters: strip() and Remove leading (left) characters: lstrip()

Use the.rstrip() method to remove only whitespace and characters at the end.

Say you only wanted to get rid of the punctuation at the end of a string.

str_greeting = "!!! Hello World!!!!" result = str_greeting.rstrip("!") print(result) #output #!!! Hello
Code language: PHP (php)

.lstrip() method is helpful when you want to remove whitespace and characters only from the start of the string.

An example for this would be removing the www. from a domain na

mydomain = "www.pc-savvy.com" result = mydomain.lstrip("w.") print(result) #output #pc-savvy.com
Code language: PHP (php)

Remove prefix: removeprefix() (Python3.9+)

.removeprefix() method returns a string with the prefix removed if it starts with the prefix given in the argument. If it doesn’t match, the original string is sent back without any changes.

str = 'abc-abc123' print(str.removeprefix('abc-')) # abc123 #If you want to execute similar processing in versions earlier than Python 3.9 def my_removeprefix(str, prefix): if str.startswith(prefix): return str[len(prefix):] else: return str print(my_removeprefix(str, 'abc-')) # abc123
Code language: Python (python)

If you use .lstrip() method, All the characters in the string given as an argument are deleted, so it can’t be used in situations where you only want to delete the prefix.

str = 'abc-abc123' print(str.lstrip('abc-')) # 123
Code language: Python (python)

Remove suffix: removesuffix() (Python3.9+)

s = '123xyz-xyz' print(s.removesuffix('-xyz')) # 123xyz #If you want to execute similar processing in versions earlier than Python 3.9 def my_removesuffix(str, suffix): return str[:-len(suffix)] if str.endswith(suffix) else str print(my_removesuffix(s, '-xyz')) # 123xyz
Code language: Python (python)

Delete a string by telling it where and how many characters to remove: Slice

You can specify the position from the back with a negative value, or you can omit it and specify from the beginning to the end.

str = '0123456789' print(str[4:7]) # 456 print(str[4:-3]) # 456 print(str[:6]) # 012345 print(str[6:]) # 6789
Code language: Python (python)

There are many reasons why you might want to remove special characters from strings in Python—for example, if you’re only interested in storing alphanumeric characters or if you want to strip away any formatting that might be present in your data set before performing further analysis on it. Whatever your reason, it’s easy to do with just a few lines of code using either the replace() method from th e str module or teh sub ()method fro mre module.. Try it out yourself and see how fast and easy it istoo get rid off pesky special characters for good!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts