How does Python compare string? Since strings are the most used data types in Python, so we thought to simplify the string comparison operations. In this tutorial, we’ll explain how to create string objects, how to use quotes with them, and most importantly the seven ways of comparing strings in Python.
Unlike Python string replace() method, we don’t need a function to compare strings. Instead, it provides a bunch of comparison operators to do the job for you. So Python makes programming easy for its users. It’s like an elastic which you can stretch but should be wise enough to not crossing the limits.
Since the scope of this blog post limits to string comparison, so we suggest reading the following Python tutorial to see a broad coverage on strings. It’ll help you learn Python to the depth.
Must Read – Python Strings Tutorial
Before we take on the seven Python compare string methods, let’s start the topic by answering some how-tos of Python string literals.
- How to create a simple string in Python?
- How to create a Unicode string in Python?
- How does Python store strings in memory?
Python has many intuitive ways of instantiating strings which could be vital for coding in a script. Let’s begin to address them before we move to learn about the Python compare string methods.
1.1- How to create a simple string in Python?
In Python, we can create strings with single quotes, double quotes, and as well as with triple quotes. When we use triple quotes, strings can span many lines without adding an escape character. Try running the below code snippet in your Python shell.
Note: For testing the Python code online, read about several online Python shells from our blog.
#!/usr/bin/python # test_strings.py str1 = "hello world" str2 = 'hello' str3 = """ we use Python """ print str1 print str2 print str3 # Output: # hello world # hello # we # use # Python #
1.2- How to create a Unicode string in Python?
If you want to make use of Unicode strings, then add a prefix with an ‘u’ or ‘U’ to the text. And for your reference here is an example.
#!/usr/bin/python # test_unicode.py str = u'\u0049 \u006c\u006f\u0076\u0065 \u0050\u0079\ \u0074\u0068\u006f\u006e \u0070\u0072\u006f\u0067\ \u0072\u0061\u006d\u006d\u0069\u006e\u0067\u002e' print str # It'll print the following output. # # I love Python programming. #
1.3- How does Python store strings in memory?
It’s a known fact that you can’t change a string after its creation. It turns immutable after instantiation. Also, for your note that you can use the id() method to know the memory location of an object in Python. It’ll help you verify the statement made here.
You can yourself check it by running the below code snippet.
str1 = "hello" str2 = 'hello' print "id(str1) =", hex(id(str1)) print "id(str2) =", hex(id(str2)) # The above code will give the output as. # # id(str1) = 0x1587f00 # id(str2) = 0x1587f00 #
The output of the given code snippet proves our point that both string variables (str1 & str2) are pointing to the same memory location.
Interestingly, if you do modify a string, then Python will produce a new string object.
str1 = "hello" print "Before change: id(str1) =", hex(id(str1)) str1 += " world" print "After change: id(str1) =", hex(id(str1)) # Output: now memory location differs after changing the string. # # Before change: id(str1) = 0x1ec8420 # After change: id(str1) = 0x1c63680 #
2- Python Compare String Methods
Python string compare methods are the easiest to use. No other programming language comes even close in comparing strings as Python does. It provides a range of operators to compare two strings.
Seven String Comparision Operators in Python:
We call these operators as Relational operators. They can compare the operands on either side of the condition.
# Seven Python string compare operators. # # 1. == => For exact match. # 2. != => Check inequality. # 3. <> => Alternate way to check inequality. # 4. < => Test if left operand is less than the right one. # 5. > => Check if left operand is greater than the right one. # 6. <= => Return true if the left operand is greater than or equal to the right one. # 7. >= => Pass if the left operand is less than or equal to the right one. #
Python compares string lexicographically i.e. by verifying the ASCII value of the characters.
if ( 'Python' == 'Python' ): print "[Python == Python] => Python is exactly same as Python." else: print "Unexpected return." if ( 'Python' != 'C++' ): print "[Python != Python] => Python is not C++." else: print "Unexpected return." if ( 'Python' <> 'Java' ): print "[Python <> Python] => Python is not Java." else: print "Unexpected return." if ( 'A' < 'B' ): print "[Python == Python] => A comes before B in the alphabet." else: print "Unexpected return." if ( 'D' > 'C' ): print "[Python == Python] => D comes after C in the alphabet." else: print "Unexpected return." if ( 'Python' <= 'python' ): print "[python <= Python] => python is either less than or equal to Python." else: print "Unexpected return." if ( 'python' >= 'Python' ): print "[Python >= python] => Python is either greater than or equal to python." else: print "Unexpected return."
When you execute the above code snippet, it’ll yield the following results.
This tutorial on “Seven Distinguished Python Compare String Methods” was in continuation of our promise to bring each topic of your interest as simple as you could perceive it. Hence, we tried to add all micro-level about Python strings.
We wish this post could have made you more informed than you were before reading it.
All the Best,