1.判断一个字符是不是敏感字符: in
1.str
v ="年龄多大了"
if "大" in v:
print("敏感")
2.list/tuple
u1=["alex",'123',11]
if "123" in u1:
print("存在")
3.dict:判断
#1.判断k1是否在字典中
info={"k1":"v1","k2":"v2"}
if "k1" in info:
print("存在")
#字典dict默认取键值
#2.判断v2是否在其中
#方案一 循环字典判断
flag="不存在"
for v in info.values():
if v == "v2":
flag="存在"
print("flag")
#方式二 强制转换列表
if "v2" in list(info.values()):
print("")
#3.判断k1:v1是否在其中
value = info.get('k1')#value=None/v1
if value == 'v1':
print("存在")
else:
print("不存在")
-
练习:敏感字符判断
#让用户输入任意字符串,然后判断字符串是否包含指定的敏感字符 char_list=["力气大","唐油光","沾沾灰"] content = input("请输入内容:") flag=True for ele in char_list: if ele in content: flag=False break if flag: print(content) else: print("包含敏感字符")