comparenocase

comparenocase的替代方法有哪些

小樊
83
2024-07-06 08:22:14
栏目: 编程语言

  1. 使用lower()函数将字符串转换为小写后再比较两个字符串
str1 = "Hello"
str2 = "hello"
if str1.lower() == str2.lower():
    print("Strings are equal ignoring case")
  1. 使用re模块中的re.IGNORECASE参数来进行正则表达式匹配时,忽略大小写
import re
str1 = "Hello"
str2 = "hello"
if re.match(str1, str2, re.IGNORECASE):
    print("Strings are equal ignoring case")
  1. 手动比较字符串的每个字符,忽略大小写
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")

0
看了该问题的人还看了