'How to check whether there is punctuations in a string or no, in python? [closed]

How can I check if there are any punctuation like (, ' " or any punctuation) in s string. In python



Solution 1:[1]

There is a good article here on how to do this: https://www.geeksforgeeks.org/string-punctuation-in-python/

Python string.punctuation is a list of all the punctuation. So you can use it like this to find out if there is any punctuation in a string:

from string import punctuation
any(char in punctuation for char in 'This is just a test')

Here is the link to the python docs for the string library https://docs.python.org/3/library/string.html#string.punctuation

As per Kelly Bundy's comment this will not test for all punctuation. So here is another solution that will test if there are any characters that are not Alpha Numeric using regex.

if re.findall(re.compile('[^a-zA-Z\d\s:]'), 'This is a - test - with punct.'):
    print('found punctuation')

But without knowing exactly you want to remove I cant provide you an exact solution.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1