Python

Remove Punctuation from String Python

In Python, while working with strings, we have an issue in which we need to delete a few characters from strings. In this guide, you’ll study how to delete punctuation from a string. We’ll also discuss how to remove punctuation from a Python string using various built-in methods. To work with manipulating Python strings is a vital skill for any Pythonista. Strings you find on the internet need a bit of work to examine. Many times, a few tasks you’ll often meet is the capability to use Python to erase punctuation from a string.

Example 1

This is the basic way in which you remove punctuation from the string. Here, we analyze the punctuations using a punctuated raw string and then we built a string erasing those punctuations. We use a loop and raw punctuated string to remove punctuation from it. To perform this task, we use the Spyder compiler in Windows 10. Come let’s demonstrate the working of erasing punctuation in Python string using loop + punctuation string.

Initially, we initialize a python string “Python, is best : for ! Learning ;”. We can then print the initially created string. Our next step is to initialize punctuation and then use For Loop to remove it from the string. In For Loop, we use the if statement to check the punctuation. If the condition is TRUE, then it moves to the next statement where replace() function removes the punctuation from the Python string. For Loop and if statement checks both the string and punctuation variable and then erase punctuation from the given string. We can then use a print statement to display the filtered string after the removal of punctuation.

my_str = "Python, is best : for ! Learning ;"
print("Our first string is : " + my_str)
punc_str = '''!()-[]{};:/[email protected]#$%^'"\,.&*_~'''
for i in my_str:
    if i in punc_str:
        my_str = my_str.replace(i, "")
print("The filtered string after removal of punctuation is : " + my_str)

To display the output, dry run the program code and view the unpunctuated string on the console screen.

Example 2

In our above method, we use For loop to remove punctuation from the string. But in this program, we remove all punctuation with a null string via regex. Regex termed as the regular expression is an arrangement of characters that describes a search design. Usually, these patterns are used to search strings using “find” or “find and replace” methods on strings. Regex is great as it comes built-in with various helpful character libraries that let us choose different types of characters.

This approach looks for anything that isn’t an alphanumeric character or blank space and swaps it with a null string, thereby eliminating it. Come, let’s demonstrate the working of erasing punctuation in Python string using regex.

Initially, we import the “re” module and then create a python string “Python, is the best : programming ! language ;”. We can then print the initially created string. Our next step is to call the re.sub() function which takes \w\s and my_str as a parameter. The \w\s in re.sub() searches words and blank spaces. But our task is to remove punctuation so we ‘^’operator with \w\s which removes punctuation from the string and then displays the resultant string on the display screen.

import re
my_str = "Python, is the best : Programming ! language ;"
print("Our original string is : " + my_str)
res = re.sub(r'[^\w\s]', '', my_str)
print("The string after removing punctuation is : " + res)

Run the code and check how we can use regex to eliminate punctuation in Python:

Example 3

In the aforementioned section of the tutorial, we discussed how to use the ForLoop() and regex() methods to remove punctuation from the string. In this section, we use the str.replace() method to replace sole punctuation. Let’s say you want to remove ‘!’ from our string, we could use the str.replace() method for this purpose. Let’s take a glance at how to:

my_string = '!hey. wh?at is your nam[e]? .'
new_string = my_string.replace('!', '')
print(new_string)

Run the code and check how to eliminate single punctuation from Python string:

Conclusion

In this guide, we discussed how to eliminate punctuation from a string. We erudite how to do this utilizing the ForLoop() function, and regular expressions. Both methods are very useful and efficient to remove punctuation from the Python string.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content