String comparison in python 3

by Alex
String comparison in python 3

A string in Python is a set of characters enclosed in quotation marks. The programming language allows string comparison operations, and the following operators are used for this: <, <=, ==, !=, >, >=. String comparison has a number of its own peculiarities, different from the comparison of numbers.

More / Less

Comparison of more or less is done using the > and < operators, respectively. For example, let’s compare strings with the word apple and banana:

print("apple" > "banana")

False
print("apple" < "banana")

True

It makes sense to assume that apple comes before banana and the letter “a” comes before “b”, which gives this result. But in fact it’s a bit more complicated. Let’s check if apple and apple are equal:

print("apple" == "apple")

False
print("apple" > "Apple")

True

The point is that identical letters in different registers are considered different characters. The computer distinguishes between characters by the unique values assigned to them. In our case, the Latin “A” has a value of 65, while the lowercase “a” has 97. You can find out the unique value of any character by using the ord function. For example:

print(ord("A"))

65

When comparing characters or strings, Python converts the characters to their respective ordinal values, then compares from left to right. In the example above, “a” is greater than “A”, respectively “apple” is greater than “Apple”. There is a chr function that converts an ordinal value to a character. Example:

print(chr(1040))

А

The result will be a Cyrillic A, which corresponds to the value 1040. Not only letters and numbers have their own values, but also auxiliary characters like ? or =. The space ” ” also has a value of 32. A human usually ignores the difference between letter cases, but the computer does not. This is solved simply by converting the string to a standard format, e.g. lower case, and then you can do the comparison.

str1 = "apple"
str2 = "apple"
str2.lower()
print(str1 == str1)

True

In this string comparison example in Python 3, we lowered str2 using the lower method. Therefore, the result of the comparison is True.

Equality

You can check the equality of a term by using the == operator . For example:

print("str1" == "str2")

False

This performs an exact comparison in Python. The two strings are not equal, so the result is False. Sometimes a situation arises where two strings contain the same characters, but they are rearranged. It is clear that such strings will not be equal. If you want to check if strings contain the same characters, but they are just rearranged, then you can do the following. Turn the string into a list and sort it. Let’s check the result of comparison and print the contents of the sorted lists.

strA = "abcde"
strB = "abdec"
print(sorted(list(strA)) == sorted(list(strB))
print(sorted(list(strA))
print(sorted(list(strB)))

True
['a', 'b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e']

Not equal

The != operator checks if the strings are not equal. For example:

print("abc" != "zxc")

True

Since abc is not equal to zxc.

More or equal / Less or equal

The operators <= and >= determine whether one string is greater than / less than or equal to another string. Simply put, if at least one condition is met, it will be True. For example:

print("abc" <= "bcd")

True

Because abc is smaller than bcd.

print("abc" >= "abc")

True

Since both lines are equal.

Related Posts

LEAVE A COMMENT