Python Compare Strings Ignore-case

Python compare strings ignore case or case-insensitive | In computers, case-sensitivity defines whether uppercase and lowercase letters are treated as distinct (case-sensitive) or equivalent (case-insensitive). The case-insensitive means the string which you are comparing should exactly be the same as a string that is to be compared but both strings can be either in upper case or lower case. (ie., different cases)

Python String equals Ignore-case

This python program using the built-in function to check string is equal or not. The lower() function converts all uppercase characters in a string into lowercase characters and the if-else statement check string are equal or not using equality operator (==).

# Python program to compare strings ignore case

# first string
string1 = 'KNOW PROGRAM'

# second string
string2 = 'Know Program'
  
# check strings are same using lower() function
if(string1.lower() == string2.lower()):
    print('The strings are the same.')
else:
    print('The strings are not the same.')

Output:-

The strings are the same.

Equal ignore case in Python

We can also write this Python program to compare between strings ignore cases using the upper() function. The upper() function converts all lowercase characters in a string into uppercase characters and the if-else statement check string is equal or not using equality operator (==).

# Python program to compare strings ignore case

# first string
string1 = 'Python'

# second string
string2 = 'PYTHON'
  
# check strings are same using upper() function
if(string1.upper() == string2.upper()):
    print('The strings are the same.')
else:
    print('The strings are not the same.')

Output:-

The strings are the same.

Python Program to Compare between Strings ignore-case using casefold()

The casefold() method works similar to lower() and upper() method. But compared to the lower() and the upper() method it performs a strict string comparison by removing all case distinctions present in a string. It is used for caseless matching, i.e. ignores cases when comparing.

For example, the German lowercase letter ‘ß’ is equivalent to ‘ss’. But every user might not know German, However, since ‘ß’ is already lowercase, the lower() method does nothing to it. But, casefold() converts it to ‘ss’.

# Python program to compare strings ignore case

# first string
string1 = 'claß'

# second string
string2 = 'class'
  
# check strings are same using lower() function
if(string1.casefold() == string2.casefold()):
    print('The strings are the same.')
else:
    print('The strings are not the same.')

Output:-

The strings are the same.

Also See:- Python program to convert uppercase to lowercase

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

1 thought on “Python Compare Strings Ignore-case”

Leave a Comment

Your email address will not be published. Required fields are marked *