In Python, a string is an immutable object. A variable is just a label given to an object in the memory. It means, if two variables are assigned the same string value, they are really referring to the same string object in memory. This fact can be verified by checking their id() value.
str1="Hello" str2="Hello" str3="HELLO" print (id(str1), id(str2), id(str3))
1215823728944 1215823728944 1215823729648
Hence, comparison operator
== for checking equality returns
True if two string operands have same id() value, and
print(str1 == str2) print(str1 == str3)
Python also has
!= operator (read as is not equal to), which obviously returns
id() values of string operands are different, and
False if same.
print(str1 != str2) print(str1 != str3)
Python also has the identity operator called
is. This operator evaluates to
id of two operands is the same.
print(str1 is str2) print(str1 is str3)
There is also
is not operator, which is exactly opposite.
print (str1 is not str2) print (str1 is not str3)
On the face of it,
== and is operators seem to behave similarly. However, consider the following example.
var1="Tutorials" var2="Teacher" var3="TutorialsTeacher" print(var1+var2 == var3) print(var1+var2 is var3)
Even though concatenation of
var1 + var2 evaluates to
var3, comparison with var3 using
True but using is returns
==, !=, <, > <= and >= perform comparison of strings according to lexicographic order of letter.
Unicode values of letters in each string are compared one by one. Result of
< operator depends on Unicode values of letters at index where they are not the same.
"bat" > "ball" returns
True, which simply means, first string appears after the second in alphabetical order.
This is because the position where the comparison of string breaks, Unicode value of
t is more than that of
print(ord('t'), ord('l')) #in first comparison print(ord('r'), ord('t')) #in second comparison
116 108 114 116
Obviously, the string comparison is case-sensitive as Unicode values of lower case letters are more than that of upper case letters. If you should compare strings without taking the case into consideration, convert them to upper or lower case.
str1="Hello" str3="HELLO" print (str1.upper()>=str3)
Finally, we take a brief look at the
search() functions defined in the
Python's re module implements regular expression syntax for finding the appearance of a pattern of letters in a string. The
match() function checks whether the given pattern is found at the beginning of a string.
On the other hand, the
search() function is able to check its presence anywhere in the string. Another function, the
findall() returns all appearances of the pattern.
import re string="Simple is better than complex" pattern="Simple" if re.match(pattern, string): print ("found match") else: print("match not found") pattern="dummy" if re.match(pattern, string): print ("found match") else: print("match not found") pattern="ple" obj=re.search(pattern, string) print ("found pattern at ", obj.start()) obj=re.findall(pattern, string) print (obj)
found match match not found found pattern at 3 ['ple', 'ple']
To find the position of each appearance of the pattern, use the
obj=re.finditer(pattern, string) for app in obj: print ("found pattern at index : ", app.start())
found pattern at index : 3 found pattern at index : 25
The re module is much more powerful with the ability to search for complex string patterns such as alphanumeric strings, ignoring case while searching, escape characters, etc. This advanced discussion is beyond the scope of this article.
- Remove duplicate items from list in Python
- Remove items from a list in Python
- repr() vs str() in Python
- How to sort dictionary by value in Python?
- globals() and locals() Method in Python
- Convert User Input to a Number
- Convert String to Datetime in Python
- How to count the occurrences of a list item?
- How to flatten list in Python?
- How to merge dictionaries in Python?