Python Program to Check Special Character

Here, we will develop a Python program to check the special character. 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.

Check if Special Character in String 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.

Python Program to check the Special Character

We are using 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: [email protected]
The string contains special characters.

Python Program to check for Special Characters in String

In the above program, we used re.match() method but in this program, we are using 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

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.

Also See:- How to Check if Two Strings are equal in Python

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

Leave a Reply