If I have two variables, a and b and they could be integers, float, or strings.
I want to return True
if they are equal (in case of string, ignore case).
As Pythonic as possible.
Java String equalsIgnoreCase() method Java equalsIgnoreCase() method is used to check equal strings in case-insensitive manner.
Approach No 1: Python String lower() Method This is the most popular approach to case-insensitive string comparisons in Python. The lower() method converts all the characters in a string to the lowercase, making it easier to compare two strings.
Use the Equals() method to compare strings case-insensitive using StringComparison parameter.
Use str. casefold() to compare two string ignoring the case. Trim strings using native methods or regex to ignore whitespaces when performing string comparison.
This is the most pythonic I can think of. Better to ask for foregiveness than for permission:
>>> def iequal(a, b):
... try:
... return a.upper() == b.upper()
... except AttributeError:
... return a == b
...
>>>
>>> iequal(2, 2)
True
>>> iequal(4, 2)
False
>>> iequal("joe", "Joe")
True
>>> iequal("joe", "Joel")
False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With