Python is a high-level programming language that is known for its easy-to-use syntax and the vast range of libraries.
Whether a beginner or a seasoned programmer, string comparison is an essential part of every programming task in Python. In this tutorial, we will explore the various ways to compare two strings in Python.
Using the == operator
The most straightforward method to compare two strings in Python is to use the ‘==’ operator. This operator checks if the two strings are equal, considering every character and letter in the string.
Example:
1 2 3 4 5 6 |
string1 = "apple" string2 = "apple" if string1 == string2: print("The two strings are equal") else: print("The two strings are not equal") |
The output of this code will be “The two strings are equal.”
Using the != operator
Similarly, we can use the “!=” operator to check if two strings are not equal. This operator works by comparing the two strings character by character and returning True or False based on the comparison.
Example:
1 2 3 4 5 6 |
string1 = "apple" string2 = "banana" if string1 != string2: print("The two strings are not equal") else: print("The two strings are equal") |
This code will print “The two strings are not equal.”
Using the string methods
Python provides several string methods that allow us to compare two strings in a more advanced way.
The casefold() method
The casefold() method returns a lowercase copy of the string, making it ideal for comparing two lowercase strings.
Example:
1 2 3 4 5 6 |
string1 = "apple" string2 = "APPLE" if string1.casefold() == string2.casefold(): print("The two strings are equal") else: print("The two strings are not equal") |
This code will print “The two strings are equal.”
The startswith() and endswith() methods
The startswith() method checks if a string starts with a specified substring. Similarly, the endswith() method checks if a string ends with a specified substring.
Example:
1 2 3 4 5 6 7 8 9 10 |
string1 = "Hello World" if string1.startswith("Hello"): print("The string starts with Hello") else: print("The string does not start with Hello") if string1.endswith("World"): print("The string ends with World") else: print("The string does not end with World") |
The output will be “The string starts with Hello” and “The string ends with World.”
Full code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
string1 = "apple" string2 = "apple" if string1 == string2: print("The two strings are equal") else: print("The two strings are not equal") string1 = "apple" string2 = "banana" if string1 != string2: print("The two strings are not equal") else: print("The two strings are equal") string1 = "apple" string2 = "APPLE" if string1.casefold() == string2.casefold(): print("The two strings are equal") else: print("The two strings are not equal") string1 = "Hello World" if string1.startswith("Hello"): print("The string starts with Hello") else: print("The string does not start with Hello") if string1.endswith("World"): print("The string ends with World") else: print("The string does not end with World") |
Output:
The two strings are equal The two strings are not equal The two strings are equal The string starts with Hello The string ends with World
Conclusion:
In this tutorial, we explored the various ways to compare two strings in Python, ranging from the simple ‘==’ and ‘!=’ operators to the more advanced string methods.
Hopefully, this tutorial will help you write more efficient code and make the most out of Python’s powerful string manipulation capabilities.