How to check if a string contains a Special Character or Not in Python

In this tutorial, you will learn how to check if a string contains a special character or not in Python programming language.

Special characters are those characters that have a built-in meaning in the programming language. These can be either a single character or a set of characters.

Here are some examples:

Python<Language
String contains Special Characters

Python Language
String does not contain Special Characters

To check the presence of special characters we create a regular expression object (string_check) of all the special characters and pass it into the search function. The search function matches all the characters of the input string to the set of special characters specified in the Regular Expression object (string_check). If there is a match then it returns the matched character otherwise it will return None.

Python program to check if a string contains a special character or not

#Python program to check if a string contains
#any special characters or not

# import required package
import re 
  
# Function checks if the input string(test)
# contains any special character or not
def check_splcharacter(test): 
  
    # Make an RE character set and pass  
    # this as an argument in compile function
 
    string_check= re.compile('[@_!#$%^&*()<>?/\|}{~:]') 
      
    # Pass the string in search function  
    # of RE object (string_check).
     
    if(string_check.search(test) == None): 
        print("String does not contain Special Characters.")
          
    else: 
        print("String contains Special Characters.") 
      
  
# Driver Code 
if __name__ == '__main__' : 
      
    # Enter the string to be checked
 
    test = "Code%Speedy"
      
    # calling check_splcharacter function
  
    check_splcharacter(test) 
    

OUTPUT:

String contains Special Characters

First, we import the required package from the Python library.




import re

We define a function check_splcharacter and pass a string argument(Test). Then create a Regular Expression (string_check) containing all the special characters using the re.compile function. Pass the argument string (ie test) in the search function.

The search function matches each character present inside the ‘test’ string with the special characters present in the regular expression. If there is a match it returns the character that matched otherwise it returns None.

If the result is None then the output will be ” String does not contain Special Characters” else the output will be “String contains Special Characters”

def check_splcharacter(test):
    # Make own character set and pass 
    # this as argument in compile method
 
    string_check= re.compile('[@_!#$%^&*()<>?/\|}{~:]')
 
    # Pass the string in search 
    # method of regex object.
 
    if(string_check.search(test) == None):
        print("String does not contain Special Characters.") 
    else: 
        print("String contains Special Characters.")


You may also read:


Leave a Reply

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