Search code examples
pythonpython-3.xfunctionspecial-charactersrestriction

How can i make two numbers 9 and 3 as special Number in Python?


I am creating a simple function to check the special character in Python. We make a Python Program to check if a string contains any special character.

import re 

def run(string): 
    regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]') 
    if(regex.search(string) == None): 
        print("String value is accepted")       
    else: 
        print("String value is not accepted.") 

if __name__ == '__main__' : 
    # Here in "money@rupay" the @ is a special character.
    string = "money@rupay"
    run(string) 
    # The Output is:- String value is not accepted.

In function, I check the special characters [@_!#$%^&*()<>?/\|}{~:] reject as String. It works perfectly.

What is the way to pass special numbers as the restricted number in python? I want to pass some number as a special number in python like- 9 and 3. How can I make 9 and 3 as a special number which is restricted in the python program?


Solution

  • Just put 9 and 3 into brackets in your regex pattern:

    import re 
    
    def run(string): 
        regex = re.compile('[@_!#$%^&*()<>?/\|}{~:93]') 
        if(regex.search(string) == None): 
            print("String value is accepted")       
        else: 
            print("String value is not accepted.")