Comparing Strings in python
Python strings are the sequence of characters delimited by single or double quotes. i.e. everything we write inside the single or double quotes python will consider it as a string. E.g. “Hello this is a string” ‘Hi this is another string’. Since Python doesn’t have a special type for characters so a character will be treated as a single letter or a single character string. Different built in functions are available for strings in python. And like other programming languages we can also compare strings in python. Different operators are available for python compare strings operations. They include: <, <=, >, >=, ==, is
Python compare strings
Python compares the two strings by comparing the ASCII value for each character in one string to the corresponding character in the other string. E.g. if we have two strings
str1 = “Hello” str2 = “David”
When we use a comparison operator on these two string the python compiler will take the first character of each string i.e. H in our case and compare the ASCII values for H and D. Since the first characters are different and have different ASCII values so python compiler will respond to the comparison equation accordingly that str1 is greater than the str2.
Note: if the first characters are same then the compiler will move to the next. And then the process continues until the strings end or compiler finds a suitable value according to the comparison operator.
Compare strings python examples
print "Hello" == "hello"
the reason the above strings are not equal is that H has a different ASCII value then h.
print "Hey" != "Hey"
since the two strings are equal.
print "Blow" < "blown"
print "flow" <= "blow"
print "flow" >= "blow"
print "flow" > "blow"
print "flow" <> "blow"
is vs =
is and = are two operators used in string comparison in python. What is the difference between the two? Python strings are immutable i.e. if we create a string and later we again create a same string in our program. The python compiler will first check whether a same string is available in the memory or not. If yes it will point the new string to already created string in the memory.
So is operator is used to check the identity of the two strings. i.e. whether the two strings are pointing to the same memory location or not.
str1 = 'hello' str2 = 'hello' print str1 is str2
This is because both the strings are pointing to a same memory location.
str1 = 'hello' str2 = str("hello") print str1 is str2
And we can use = to actually check whether the two strings are same or not. Rather than checking their identity.
str1 = 'hello' str2 = ''.join(['h', 'e', 'l', 'l', 'o']) print str1 == str2
As we can see that both the strings contain the same characters. So the two strings are equal.
But for the above string if we do the following
str1 = 'hello' str2 = ''.join(['h', 'e', 'l', 'l', 'o']) print str1 is str2 print str1 == str2
This is because the two strings are equal but they are not pointing to the same memory location.
Note: All these string comparison operators can be used with the conditional operators in python.