In this article, we will learn how to compare strings in python. We will discuss why we need to compare strings, and what are some string comparison operators in python, with examples and source code!
What are Strings?
A string is generally a sequence of characters. A character is the building block of a string. It could be a letter, a number, or a symbol. For example, in the English Language, we have 26 alphabets available, each of which is an alphabet.
But the computer system does not understand characters and hence it deals with binary numbers. All the characters that we see on our screen are internally stored and manipulated as a combination of 0s and 1s.
The conversion of characters and the binary number is called encoding, and the reverse of this is known as decoding. Some of the popular encodings are ASCII and Unicode. In the Python programming language,
So, we can define a string as a sequence of Unicode characters, or in layman's terms, a combination of alphabets, numbers, or symbols that are enclosed between two quotation marks.
What is the need to compare strings?
It is a very operation that every python programmer should learn. Comparing strings is important to check if two strings are the same or not. We can use it to verify if two strings are equal and perform a certain task only if they are equal. It is very important for search functionality.
Let us say you created a student database for a university. If you wanted to access the records of a particular student you would have to search for them in the database. In order to confirm that the record you found is the one you wanted, you would have to match the name of the record with the name you had in mind.
In such scenarios, you need to compare two strings. Similarly for situations like checking for a valid email address, checking the name that exists in a record, etc; we require string comparisons.
Python String Comparison operators
In python language, we can compare two strings such as identifying whether the two strings are equivalent to each other or not, or even which string is greater or smaller than each other. Let us check some of the string comparison operators used for this purpose below:
- ==: This operator checks whether two strings are equal.
- !=: This operator checks whether two strings are not equal.
- <: This operator checks whether the string on the left side is smaller than the string on the right side.
- <=: This operator checks whether the string on the left side is smaller or equal to the string on the right side.
- >: This operator checks whether the string on the left side is greater than the string on the right side.
- >=: This operator checks whether the string on the left side is greater than the string on the right side.
Let us take an example to understand these operators:
string1 = "Abrar" string2 = "Ahmed" string3 = "ABCD" string4 = "ABCD" if string1 <= string2: print(string1," is smaller ",string2," is greater") if string2 >= string4: print(string4," is smaller ", string2," is greater") if string3 == string4: print(string3," is equal to ",string4) if string1 != string3: print(string1," is not equal to ", string3)
Abrar is smaller Ahmed is greater ABCD is smaller Ahmed is greater ABCD is equal to ABCD Abrar is not equal to ABCD
String Equals Check in Python
In python, we can check whether strings are equal or not using two methods. The first method is to use the relational equality operator "==" to perform the string comparison.
The second method is to use a dedicated string function to perform comparisons, the __eq__() function. It is a magic function defined in the string class and compares two strings to return True if they are equal or Fale if they are not.
s1 = 'String' s2 = 'String' s3 = 'string' # case sensitive equals check if s1 == s2: print('s1 and s2 are equal.') if s1.__eq__(s2): print('s1 and s2 are equal.')
Here, we check strings s1 and s2 whether are equal or not and then use the “if” conditional statement with a combination of the equal operator.
s1 and s2 are equal. s1 and s2 are equal.
What about Case insensitive comparisons?
While checking the equality in strings sometimes we wish to ignore the case of the string while comparing. This can be done by bringing both the strings that we wish to compare in one particular case, ie, we could make both the strings uppercase or lowercase and then perform the comparison.
The cases of the strings can be changed with the help of three in-built functions, casefold(), upper(), and lower(). Both casefold() and lower() methods convert the string to lowercase.
But the difference is casefold() is more aggressive and can convert more letters to lowercase than lower(). The upper() method can be used to convert all the characters in the string to uppercase.
s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')
string string s1 and s3 are equal in case-insensitive comparison string string s1 and s3 are equal in case-insensitive comparison STRING STRING s1 and s3 are equal in case-insensitive comparison
String comparison can be done either case sensitive or not. For case-sensitive comparisons, we use the equality operator or the __eq__() function. For case-insensitive comparisons, we can change all the characters in the string to lowercase using casefold() or lower() methods.
We could also convert all the characters to upper case using the upper() method.
There are various situations in programming that require us to perform string comparisons like verification or searching for a name, etc. Now you know how to compare strings in python? It is on you now to do it yourself and run the above source code on your system.1