Python Compare Strings

Posted in /  

Python Compare Strings
vinaykhatri

Vinay Khatri
Last updated on April 25, 2024

    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 expressions but using comparison operators, we can also perform some basic comparisons 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.

    1. Python String comparison for equality ( == )

    The double equal to the sign == in Python represent the Equal operator. This operator checks if both values are equal or not. If both the values are equal, it returns True. Else, it returns False. We can also use the == operator between two string values and check if both 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 string data is the 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 the operator is just the opposite of the equal to operator. To represent this operator in Python, we use the Exclamation sign followed by equal to sign != . This operator returns False if both the values are the 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 compares the data values but their cases as well.

    Example 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: Using Greater than operator ( > )

    The Greater than operator checks 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 the operator > , first, follow the dictionary ranking approach if the cases of both the values are the same.

    If the cases are different, then it uses the character Unicode value to compare which character is greater. The Unicode values of lower case alphabets (a-z) are 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 is different, then to compare the operator of the value, 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: Using less than operator ( < )

    The less than operator checks if the values on the left side of the operator are less than the values on the right side. Similar to the greater than the operator, less than operator is also case sensitive. To compare the values, it follows 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: Using less than equal to operator ( <= )

    The less than equal to operator checks if the string value on the left side of the operator is less than or equal to the value on the right side. This operator is an extension of less than > operator. It returns True for all the comparison where the left value is less than or equal to the 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: Using greater than equal to operator ( >= )

    The greater than equal to the operator >= is used to check if the value on the left side of the operator is greater 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 Comment on this Post

    0 Comments