Quantcast
Channel: How do I do a case-insensitive string comparison? - Stack Overflow
Viewing all articles
Browse latest Browse all 16

Answer by Harley Holcombe for How do I do a case-insensitive string comparison?

$
0
0

Assuming ASCII strings:

string1 = 'Hello'string2 = 'hello'if string1.lower() == string2.lower():    print("The strings are the same (case insensitive)")else:    print("The strings are NOT the same (case insensitive)")

As of Python 3.3, casefold() is a better alternative:

string1 = 'Hello'string2 = 'hello'if string1.casefold() == string2.casefold():    print("The strings are the same (case insensitive)")else:    print("The strings are NOT the same (case insensitive)")

If you want a more comprehensive solution that handles more complex unicode comparisons, see other answers.


Viewing all articles
Browse latest Browse all 16

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>