Python Compare Strings

By | October 19, 2021

There are 6 comparison operators in Python that return True or False by comparing two operands. And these 6 operands can also compare any two string values. We often come across many problems in programming where we need to compare two string values, and in most of the programming, we need to take the help of regular expression. Although in Python we also have regular expression but using comparison operators we can also perform some basic comparison between two string values.

Python Compare Strings

In this Python tutorial, we will walk through all the comparison operators available in Python to compare string values.

Vamware

1. Python String comparison for equality (==)

The double equal to sign == in Python represent the Equal operator. This operator checks if both the values are equal or not.

If both the values are equal it return True else it return False. We can also use the == operator between two string value and check if both the values are equal.

Example 1

string_1 = "Hello"
string_2 = "Hello"

print("Is string_1 == string_2?")

# comapare equality between string1 and string2
print(string_1==string_2)

Python comparison operators are case sensitive if the strings data is same but their cases are different the comparison operator will return False.

Example 2

string_1 = "Hello"
string_2 = "hello"

print("Is string_1 == string_2?")

# comapare equality between string1 and string2
print(string_1==string_2)

Output

Is string_1 == string_2?
False

2. Python String comparison: Using Not Equal to operator (!=)

The not equal to operator is just opposite of eqaul to operator. To represent this operator in Python we use the Exclamation sign followed by equal to sign !=.

This operator return False if both the values are same(case and data vise), but if both the values are different it will return True.

This operator is used to check if both the string values are not equal.

Example 1

string_1 = "Hello"
string_2 = "Hello"

print("Is string_1 != string_2?")

# comapare not equality between string1 and string2
print(string_1!=string_2)

Output

Is string_1 != string_2?
False

!= operator is also case sensitive, and it not only compare the data values but their cases as well.

Exampl 2

string_1 = "Hello"
string_2 = "hello"

print("Is string_1 != string_2?")

# comapare not equality between string1 and string2
print(string_1!=string_2)

Output

Is string_1 != string_2?
True

3. Python String comparison: Uisng Greater than operator (>)

The Greater than operator check if the string value on the left side is greater than the string value on the right side.

To compare both the values greater than operator > first follow the dictionary ranking approach if the cases of both the values are same. If the cases are different then it use the character unicode value to compare which character is greater.

The unicode values of lower case alphabets (a-z) is greater than the Upper case alphabets(A-Z).

Example 1

string_1 = "a"
string_2 = "b"

print("Is string_1 > string_2?")

# comapare grater than value s
print(string_1>string_2)

Output

Is string_1 > string_2?
False

Example 2:

If the case of both the string values are different then to compare the values operator use the unicode values of the characters.

string_1 = "a"
string_2 = "A"

print("Is string_1 > string_2?")

# comapare greater than value s
print(string_1>string_2)

Output

Is string_1 > string_2?
True

4. Python String comparison: Uisng less than operator (<)

The less than operator check if the values on the left side if the operator is less than the values on the right side.

Similar to the grater than operator less than operator is also case sensitive, to compare the values it follow the dictionary ranking approach along with comparing the Unicode values of the characters.

Example 1

string_1 = "a"
string_2 = "A"

print("Is string_1 < string_2?")

# comapare less than values
print(string_1<string_2)

Output

Is string_1 < string_2?
False

Example 2

string_1 = "at"
string_2 = "be"

print("Is string_1 < string_2?")

# comapare less than values
print(string_1<string_2)

Output

Is string_1 < string_2?
True

5. Python String comparison: Uisng less than equal to operator (<=)

The less than equal to operator check if the string value on the left side of operator is less than or equal to the value on right side.

This operator is an extension of less than > operator. It return True for all the comparison where the left value is less than or equal to teh right value.

Example 1:

string_1 = "at"
string_2 = "be"

print("Is string_1 <= string_2?")

# comapare less than equal to values
print(string_1<=string_2)

Output

Is string_1 <= string_2?
True

Example 2:

string_1 = "at"
string_2 = "at"

print("Is string_1 <= string_2?")

# comapare less than equal to values
print(string_1<=string_2)

Output

Is string_1 <= string_2?
True

6. Python String comparison: Uisng greater than equal to operator (>=)

The greater than equal to operator >= is used to check if the value on the left side of the operator is grater than or equal to the value on the right side of the operator.

And similar to other comparison operators, it uses the dictionary ranking followed by the unicode of the characters to compare the string values.

Example 1

string_1 = "at"
string_2 = "at"

print("Is string_1 >= string_2?")

# comapare greater than equal to values
print(string_1>=string_2)

Output

Is string_1 >= string_2?
True

Example 2:

string_1 = "at"
string_2 = "be"

print("Is string_1 >= string_2?")

# comapare greater than equal to values
print(string_1>=string_2)

Output

Is string_1 >= string_2?
False

Wrapping Up!

Python comparison operators are very powerful when it comes to comparing different data type objects. In other programming languages, you might have to put the logic if you wish to compare two string values, but in Python, we can perform it within a single line using the comparison operators.

The comparison operator uses the dictionary ranking along with Unicode or ASCII values to compare the string values.

People are also reading:

Leave a Reply

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