| by Arround The Web | No comments

Python string comparison

Comparing Strings involves testing whether two or more strings have the same content. Python has several built-in methods and operators for comparing strings. In Python programming, you can compare strings utilizing the “Comparison Operators” or the “String Methods”. The comparison operators are utilized to evaluate the values of two strings, while the string methods are used to compare the properties of two strings.

This article aims to reveal a step-by-step approach to how to compare strings in Python.

Method 1: Compare Strings in Python Using “Comparison Operators”

To compare two values and get a “true” or “false” result, we use comparison operators in Python. They are as follows:

  • <” (less than)
  • >” (greater than)
  • <=” (equal to or less than)
  • >=” (greater than or equal to)
  • ==” (equal to)
  • !=” (not equal to)

These operators are also known as “Relational Operators”. Let’s use some of these operators to compare two strings in Python.

Example 1: Using “==” Operator

The “==” operator is utilized to determine whether two strings are equal. It retrieves “true” if the strings are equal and “false” otherwise. Here is an example code:

string1 = "Python"
string2 = "Python"
if string1 == string2:
    print("The strings are equal")
else:
    print("The strings are not equal")

 

In the above code, the “==” operator compares two initialized strings and retrieves the corresponding boolean value.

Output

Based on the above output, it can be concluded that the strings are equal.

Example 2: Using “!=” Operator

The “!=” operator is used to check if two strings have different values. When the strings aren’t equal, it retrieves “true” and when they are equal, it retrieves “false“. Here is an example code:

string1 = "Python"
string2 = "Guide"
if string1 != string2:
    print("The strings are not equal")
else:
    print("The strings are equal")

 

In this code, the “!=” operator is utilized to compare two strings and evaluate them based on the applied “if/else” statement.

Output

According to the above output, it can be implied that the strings are not equal.

Method 2: Compare String in Python Using “is” and “is not” Operators

An “is” operator determines whether two variables refer to the same memory object and the “is not” operator, however, does the opposite of the “is” operator. It checks if two variables point to different memory objects.

Example

Let’s overview the below code:

string1 = "Python"
string2 = "Guide"
string3 = "Python"
print('string1 is equal to string 2: ',string1 is string2)
print('string1 is equal to string 3: ',string1 is string3)
print('string1 is not equal to string 2: ',string1 is not string2)
print('string1 is not equal to string 3: ',string1 is not string3)

 

In the above example code, multiple strings are compared using the “is” and “is not” operators. If the strings match, the boolean value “True” is retrieved while the boolean value “False” is returned if the strings do not match.

Output

According to the above output, the “true” and “false” values indicate whether the strings are equal or not.

String Comparison Techniques

In Python, each letter is treated differently based on its case, which signifies that it differentiates between the uppercase and lowercase letters. Therefore, while comparing two strings, Python considers the case of the letters.

Example 1: Case-sensitive Comparison

The following code shows the comparison of case-sensitive strings:

string1 = "Python"
string2 = "python"
if string1 == string2:
    print("The strings are equal")
else:
    print("The strings are not equal")

 

In the above code block, the “==” operator is used with the “if” statement to check whether the initialized strings are equal or not.

Output

This output shows that the strings are not equal.

Example 2: Case-insensitive Comparison

To perform a case-insensitive comparison in Python, both strings should be converted to lowercase or uppercase utilizing the “lower()” or “upper()” methods, respectively before comparing them. Here is a code:

string1 = "Python"
string2 = "python"
if string1.lower() == string2.lower():
    print("The strings are equal")
else:
    print("The strings are not equal")

 

In this code, the “lower()” method is used to convert both the initialized strings into lowercase letters and then compare the strings via the “==” operator.

Output

Based on the output above, it can be verified that the strings are equal.

Conclusion

To compare strings in Python, various methods such as the “Comparison Operators” or the “is” and “is not” operators are used. This article has also covered the techniques for performing case-sensitive and case-insensitive comparisons using appropriate examples. To handle the case-sensitive strings “lower()” and “upper()” methods are used in Python. This blog elaborated on comparing the strings in Python.

Share Button

Source: linuxhint.com

Leave a Reply