String Comparison In Python

What we are going to see through this article is we will see in detail about String Comparison In Python. In Python, strings use the ASCII value of characters to compare. Python uses objects with the same values ​​in memory, which makes it faster to compare objects. Equivalent to some basic comparator operator (= =) and ‘is’ operator.

The following table shows the string comparison operators explained in this guide:

OperatorWhat does it do?
string1 == string2Checks if two strings are equal
string1 != string2Checks if two strings are not equal
string1 < string2Checks if string1 comes before string2 alphabetically
string1 > string2Checks if string1 comes after string2 alphabetically

Below, We can use three methods to compare the strings in the below program. Those are:

  • Relational Operators,
  • is and is not,
  • Creating a user-defined function.
String Comparison In Python

String Comparison in Python using Relational Operators

“Code” == “Code” will return True as the Unicode of all the characters are equal



In case of “Code” and “code” as the unicode of C is \u0043 and of c is \u0063
“Code” < “code” will return True and
“Code” > “code” will return False

The program code

print("Code" == "Code")
print("Code" < "code")
print("Code" > "code")
print("Code" != "Code")

Output

This is an example output for your reference.

True
True
False
False

String Comparison in Python using is and is not

str1 = "Code"
str2 = "Code"
str3 = str1

print("ID of str1 =", hex(id(str1)))
print("ID of str2 =", hex(id(str2)))
print("ID of str3 =", hex(id(str3)))
print(str1 is str1)
print(str1 is str2)
print(str1 is str3)

str1 += "s"
str4 = "Codes"

print("\nID of changed str1 =", hex(id(str1)))
print("ID of str4 =", hex(id(str4)))
print(str1 is str4)

Output

Finally, the print() function will help to display the result on the screen.

ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False

String Comparison in Python to Creating a user-defined function

Instead of using all the above methods, We can create a user-defined function to perform a task here.

# function to compare string
# based on the number of digits
def compare_strings(str1, str2):
	count1 = 0
	count2 = 0
	
	for i in range(len(str1)):
		if str1[i] >= "0" and str1[i] <= "9":
			count1 += 1
	
	for i in range(len(str2)):
		if str2[i] >= "0" and str2[i] <= "9":
			count2 += 1
	
	return count1 == count2


print(compare_strings("123", "12345"))
print(compare_strings("12345", "codes"))
print(compare_strings("12codes", "codes12"))

Read Also: Python Add Item To Array

Output

This is an example output for your reference.

False
False
True

Final Words

What we learned through this article is String Comparison In Python. Also if you have any doubts please leave a comment via the comment box. And we ask that you benefit everyone who shared this article with your friends.

Leave a Comment