How to Do Case Insensitive String Comparison in Python
Created
Modified
Using casefold Method
The str.casefold()
method returns a casefolded copy of the string. Casefolded strings may be used for caseless matching. For example,
#!/usr/bin/python3
a = "Hello"
b = "heLLo"
print(a.casefold() == b.casefold())
print(a == b)
print("ß".casefold() == "SS".casefold())
True False True
The German lowercase letter 'ß' is equivalent to "ss".
Using lower Method
The following example should cover whatever you are trying to do:
#!/usr/bin/python3
a = "Hello"
b = "heLLo"
print(a.lower() == b.lower())
print(a == b)
print('ß'.lower() == 'SS'.lower())
True False False