在处理用户输入时,可以使用以下方法来忽略大小写:
user_input = input("请输入一个字符串:")
if user_input.lower() == "hello":
print("用户输入了 hello")
import re
user_input = input("请输入一个字符串:")
if re.match("hello", user_input, re.IGNORECASE):
print("用户输入了 hello")
user_input = input("请输入一个字符串:")
expected_values = ["hello", "world", "python"]
if user_input.lower() in [value.lower() for value in expected_values]:
print("用户输入了预期值之一")
无论使用哪种方法,都可以确保对用户输入进行大小写不敏感的比较。
上一篇:不区分大小写的用户名检查?
下一篇:不区分大小写的用户输入字符串