Compare strings in Python ignorance case

Python String

In this post, we are going to learn how to compare strings in Python by ignoring the case. We can compare string in python using equality operator(==) or comparison operator (<,>,<=,>=,!=).We are going to perform string comparison using these ways.

Compare string using == operator

we are performing string comparison using the == operator each character of both strings will compare character by character. == operator returns True if both strings are equal else returns false.

Example:Compare String using == operator

greet_str = "Hi how are you"

greet_str2 = "Hi how are you"


if greet_str == greet_str2:

  print('greet_str and  greet_str2 are same')

else:
  print('greet_str and  greet_str2 not same')

Output

greet_str and  greet_str2 are same

Compare String Using IS vs == operator

Equal operator(==) compares variables based on their values mainly used for immutable type.

Is operator: It compares variables based on object id. If two variables referring the same object then they are equal. IS used to compare the objects.

var_a  = 'rat'
var_b = 'rat'


if var_a is var_b:

 print('var_a and var_b are same')

else:
  print('var_a and  var_b not same')

print('\nID of both is same:')
print(id(var_a))
print(id(var_b))

# now changing value of 

print('\n---After change value of var_b:---')

var_b = "rat is".split()[0]
print('\nvalue of var_b:',var_b)
print('\nvalues is same but ID of both is not same:\n')
print(id(var_a))
print(id(var_b))


#compare change value using is and == operator

print('\ncompare using is operator:\n')

if var_a is var_b:

  print('var_a and var_b are same\n')
else :
 print('var_a and var_b are not same\n')

 #compare change value using == operator


print('compare using == operator :\n')
if var_a == var_b:

   print('var_a and var_b are same')

 

Output

var_a and var_b are same

ID of both is same:
140037812604272
140037812604272

---After change value of var_b:---

value of var_b: rat

values is same but ID of both is not same:

140037812604272
140037812604592

compare using is operator:

var_a and var_b are not same

compare using == operator :

var_a and var_b are same

Compare strings using != operator

In this example, we are comparing string using the != ((not equal)operator. != operator return True if both strings are not equal Else return False.

greet_str = "Hi how are you"

greet_str2 = "Hi how are you"


if greet_str != greet_str2:
  print('greet_str and  greet_str2 not same')  
else:
  print('greet_str and  greet_str2 are same')

Output

greet_str and  greet_str2 are same

Compare strings using comparison operator

The string is compared character by character if the characters do not match then the character’s Unicode value is compared.

Let understand how to compare string using comparison operators

str = "Hi how are you"

str2 = "how are you"


print (str<str2)
print (str>str2)
print (str<=str2)
print (str>=str2)

Output

True
False
True
False

Compare string in Python by ignoring case

if we have to compare two strings, which are different by cases, contain the characters of different cases. In this case, we ignore the case by converting the string in lower case or in the upper case finally compares the string using the == operator.

Compare string by ignore case

greet_str = "HI how are YOU"

greet_str2 = "Hi how Are you"


if greet_str.upper()== greet_str2.upper():
   print('greet_str and  greet_str2 are same')
  
else:
  print('greet_str and  greet_str2 not same')  
 

Output

greet_str and  greet_str2 are same

Compare strings using Regex by ignorance case

In this code example we are using the regular expression to compare two strings,if the string are same then it return True else False.

import re

greet_str = "HI how are YOU"

greet_str2 = "Hi how are you"


if (re.search(greet_str,greet_str2,re.IGNORECASE)):
   print('greet_str and  greet_str2 are same')
  
else:
  print('greet_str and  greet_str2 not same')

Output

greet_str and  greet_str2 are same

Compare List of string Using Regex by ignore-case

To use Regular Expression(regex) first import re module and define a pattern, pattern matches the string in str_list that contain the characters or digits either lower or upper case.

import re

str_list = ['The96500','has123','had','HELLO123']
pattern = re.compile("(?i)[A-Z][0-9]\w+")

for str in str_list:
    if pattern.search(str):
        print (" Matched string :" ,str)

Output

 Matched string : The96500
 Matched string : has123
 Matched string : HELLO123

Conclusion

We have understood different ways to Compare strings in the Python ignorance case.