How to Check if a String Contains Special Characters in Python

Here, we will develop Programs for How to check if a string contains special characters in Python. A special character is a character that is not an alphabetic or numeric character. Non-alphabetic or non-numeric character, such as @, #, $, %, &, * and +. We are going to write a program that checks whether a string contains any special character or not using various methods.

How to Check Special Characters in Python

We will first import the required package from the Python library and take a string while declaring the variables. Then, check the presence of special characters and pass it into the search function. The search function matches all the characters of the string to the set of special characters. If there is a match then it returns the matched character otherwise it will return None.

# Python program to check special character

# import required package
import re

# take inputs
string = input('Enter any string: ')
 
# special characters
special_char = re.compile('[@_!#$%^&*()<>?/\|}{~:]')

# check string contains special characters or not
if(special_char.search(string) == None):
    print('String does not contain any special characters.')
else:
    print('The string contains special characters.')

Output:-

Enter any string: @knowprogram
The string contains special characters.

Enter any string: Know Program
String does not contain any special characters.

Enter any string: $25
The string contains special characters.

How to Identify Special Characters in Python

We are using the re.match() function to check whether a string contains any special character or not. The re.match() method returns a match when all characters in the string are matched with the pattern and None if it’s not matched.

# Python program to check special character

# import required package
import re

# take inputs
string = input('Enter any string: ')

# check string contains special characters or not
if(bool(re.match('^[a-zA-Z0-9]*$', string)) == True):
    print('String does not contain any special characters.')
else:
    print('The string contains special characters.')

Output:-

Enter any string: Pyth@n
The string contains special characters.

How to Check if a String Contains Special Characters in Python

In the above program, we used the re.match() method but in this program, we are using the re.search() method. This is also a function in the RegEx module. The re.search() function locates a match anywhere in the string.

# Python program to check special character

# import required package
import re

# take inputs
string = input('Enter any string: ')

# check string contains special characters or not
if(bool(re.search('^[a-zA-Z0-9]*$', string)) == True):
    print('String does not contain any special characters.')
else:
    print('The string contains special characters.')

Output:-

Enter any string: Python
String does not contain any special characters.

Function to Check Special Characters in Python

Function to check special Characters. The string.punctuation is pre-defined in the string module of Python3. It contains all the characters as a string. This returns all sets of punctuation.

# Python program to check special character

# importing string function
import string

# take inputs
ch = input('Enter any string: ')

# special characters
invalid_char = set(string.punctuation)

# check string contains special characters or not
if any(char in invalid_char for char in ch):
    print('String does not contain any special characters.')
else:
    print('The string contains special characters.')

Output:-

Enter any string: string.punctuation
The string contains special characters.

Get notes to make your learning process easy. These are specially designed for beginners who want to learn coding through simple words, programs, and examples. You can use it as your reference and for revision purposes.

Notes Available:- Python, Java, C/C++, DSA, SQL, HTML CSS JavaScript, etc…

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *