Example string contains case insensitive in Python
Simple example code. The list of strings matches with if there is an element of the list of strings that matches the string character for character.
list1 = ["Apple", "Lenovo", "HP", "Samsung", "ASUS"] s = "lenovo" s_lower = s.lower() res = s_lower in (string.lower() for string in list1) print(res)
Python string match ignore-case
Convert strings into lower or upper case. This is if you’re doing the exact comparison.
str1 = "Hello" str2 = "HELLO" if str1.lower() == str2.lower(): print('Both Strings are same') else: print('Strings are not same')
Comparision string case insensitive
if you’re doing a substring comparison.
str1 = "Messi is the best SoCceR player" if "soccer" in str1.lower(): print("Contain soccer") if "Player" in str1: print("Contain football")
How to make a string case insensitive in python
Answer: Make string lowercase or uppercase before matching it.
if thing.lower() == "text":
Do comment if you have any doubts and suggestions on this Python string topic.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.