阅读量:129
- 使用lower()函数将字符串转换为小写后再比较两个字符串
str1 = "Hello"
str2 = "hello"
if str1.lower() == str2.lower():
print("Strings are equal ignoring case")
- 使用re模块中的re.IGNORECASE参数来进行正则表达式匹配时,忽略大小写
import re
str1 = "Hello"
str2 = "hello"
if re.match(str1, str2, re.IGNORECASE):
print("Strings are equal ignoring case")
- 手动比较字符串的每个字符,忽略大小写
str1 = "Hello"
str2 = "hello"
if len(str1) == len(str2) and all(char1.lower() == char2.lower() for char1, char2 in zip(str1, str2)):
print("Strings are equal ignoring case")