python check if identifier defined code example

Example: check if correct identifier in python

# Python program to identify the identifier 
  
# import re module 
  
# re module provides support 
# for regular expressions 
import re 
  
# Make a regular expression 
# for identify valid identifier 
regex = '^[A-Za-z_][A-Za-z0-9_]*'
      
# Define a function for 
# identifying valid identifier 
def check(string):  
  
     # pass the regualar expression 
     # and the string in search() method 
    if(re.search(regex, string)):  
        print("Valid Identifier")  
          
    else:  
        print("Invalid Identifier")  
      
  
# Driver Code  
if __name__ == '__main__' :  
      
    # Enter the string  
    string = input("Enter you Identifier\n")
    # calling run function  
    check(string)