Computer Science, asked by mukuldembla7858, 1 year ago

How do I do a case insensitive string comparison in Python?

Answers

Answered by DayDreamer2943
1

To allow case-insensitive comparisons Python offers special string methods such as upper() and lower(). Both of them are directly available as methods of the according string object.

upper() converts the entire string into uppercase letters, and lower() into lowercase letters, respectively. Based on Listing 1 the next listing shows how to use the lower() method.

The input:

# using the == operator

listOfPlaces = [ "Berlin", "Paris", "Belgium" ]

currentCity = "Belgium"

for place in listOfPlaces :

print ("comparing %s with %s : %s " % (place, current City, place.lower() == currentCity.lower()))

The output :

$ python3 comparing-strings-case-insensitive.py

comparing Berlin with Belgium : False

comparing Paris with Belgium : False

comparing Belgium with Belgium : True

Mark me as the brainliest ❤

Similar questions