Python strings are sequences of characters. Characters are the letters or alphabets in a word. There are no separate data types for the definition of characters in Python. The string in Python is composed of Unicode code characters.
The string comparison in Python identifies whether two strings are equivalent/equal to each other or not. It also determines which string is greater and which one is smaller than the other.
We can perform Python String Comparison using Comparison operators like ==, !=, <, >, <=, >= . The characters in strings are compared character by character. There are no special methods for the comparison of strings.
This article will see how to compare Strings in Python along with their code Snippet and output.
The following different techniques are used for the comparison of strings in Python.
- Using Relational Operators ( ==, <, >, != )
- Using is and is not
- Using sorted( ) method
- Creating a user-defined function
- Comparison with user-input string
Let us look at each technique one by one.
Technique # 01 : Using Relational Operators ( ==, <, >, != )
Example Code # 1:
fruit = "Mango" print(fruit == "Mango") print(fruit != "Mango")
Example Code # 2:
country1 = "Turkey" country2 = "Cuba" print(country1 < country2) print(country1 > country2) print(country1 <= country2) print(country1 >= country2)
Technique # 02: Using is and is not
When we use the == operator, it checks for equality in the values of string operands. But using is operator identifies if both operands are referring to the same object or not. The same difference applies for is not. The code returns the boolean value in the output for this method. The following code example will help to understand it better.
list_1 = ['is', 'am', 'are'] list_2 = list_1 list_3 = list(list_1) print(list_1 == list_2) print(list_1 == list_3) print(list_1 is list_2) print(list_1 is list_3) print(list_1 != list_2) print(list_1 is not list_2) print(id(list_1)) print(id(list_2)) print(id(list_3))
Technique # 03 : Using sorted( ) method
In comparing strings with different order of characters, we need first to sort them, and then the comparison of the string will take place. Let’s have a look at the following example.
name1 = ‘Software Engineer’ name2 = ‘Engineer Software’ if sorted(name1) == sorted(name2): print ("First and second strings are equal.") else: print ("First and second strings are not the equal.")
Technique # 04: Creating a user-defined function
We use user-defined functions for comparing two strings according to any other parameter. In contrast, relational operators can only compare strings with the Unicode. The following code is the proper interpretation of this method.
def compare_strings(color1, color2): count1 = 0 count2 = 0 for i in range(len(color1)): if color1[i] >= "0" and color1[i] <= "9": count1 += 1 for i in range(len(color2)): if color2[i] >= "0" and color2[i] <= "9": count2 += 1 return count1 == count2 print(compare_strings("blue", "yellow")) print(compare_strings("green", "6789")) print(compare_strings("11orange", "red12"))
Technique # 05: Comparison with user-input string
In this technique, we take input strings from the user and then compare them using different string comparison operators.
fruit1 = input("Enter your favorite fruit1: ") fruit2 = input("Enter your favorite fruit2: ") fruit3 = fruit1 print(fruit1 == fruit2) print(fruit1 != fruit2) print(fruit1 <= fruit2) print(fruit1 >= fruit2) print(fruit1 is fruit2) print(fruit1 is not fruit2) print(fruit1 is fruit3)
In this article, we have gone through different methods of String comparison in Python. We have also learned differences among other techniques for the comparison of strings. I hope this tutorial will help you to solve programming difficulties in your developer’s life.