python怎么判断字符串中包含特殊符号

Python (223) 2023-07-24 19:35:43

python判断字符串中包含特殊符号的方法:首先使用“for i in string”命令来遍历输入的字符串;然后输入“if i in input_psd”命令对输入的语句进行判断即可。

这里不导入其他模块,预先定义好你所认为是特殊字符的字符就好

input_psd=input("请输入字符串")
#判断是否有特殊字符
string="~!@#$%^&*()_+-*/<>,.[]\/"
foriinstring:
ifiininput_psd:
print("您的输入包含特殊字符")

或者导入python内置模块re

importre
input_psd=input("请输入字符串")
test_str=re.search(r"\W",input_psd)
iftest_str==None:
print("没有没有真没有特殊字符")
else:
print("该文本包含特殊字符")

推荐课程:Python从零开始学编程(黑马程序员)

THE END

发表回复