微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Python 中将两个数字 9 和 3 作为特殊数字?

如何解决如何在 Python 中将两个数字 9 和 3 作为特殊数字?

我正在创建一个简单的函数来检查 Python 中的特殊字符。我们编写了一个 Python 程序来检查字符串是否包含任何特殊字符。

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.

函数中,我检查特殊字符 [@_!#$%^&*()<>?/\|}{~:] 拒绝为字符串。 它完美运行。

在python中将特殊数字作为限制数字传递的方法是什么? 我想在 python 中传递一些数字作为特殊数字 - 93。如何将 9 and 3 设为在 python 程序中受限的特殊数字?

解决方法

只需将 93 放在正则表达式模式中的方括号中:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。