Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
67 views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/65867797/how-can-i-make-two-numbers-9-and-3-as-special-number-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.") 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...