Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

How to Compare Strings in Python

Learn how to compare strings in Python with this step-by-step tutorial, covering the basics of string comparison and providing practical examples.| …


Updated May 14, 2023

|Learn how to compare strings in Python with this step-by-step tutorial, covering the basics of string comparison and providing practical examples.|

Introduction

Comparing strings is a fundamental operation in Python programming that can be used in various scenarios, such as validating user input, checking if two passwords match, or verifying if a password has been changed.

In this article, we will delve into the concept of comparing strings in Python, explore different methods for comparison, and provide practical examples to help you understand how to apply these concepts in your own projects.

Definition: What is String Comparison?

String comparison is the process of determining whether two or more strings are equal or not. This can be done using various methods, including:

  • Exact match: Comparing two strings character by character.
  • Case-insensitive comparison: Ignoring case differences between strings.
  • Sub-string matching: Checking if one string contains another.

Step-by-Step Explanation: How to Compare Strings in Python

To compare strings in Python, you can use the following methods:

Method 1: Exact Match

You can compare two strings using the == operator. This method checks each character of both strings and returns True if they are identical.

Example Code:

string1 = "Hello"
string2 = "Hello"

if string1 == string2:
    print("The strings are equal.")
else:
    print("The strings are not equal.")

In this example, the strings "Hello" and "Hello" are compared using the == operator. Since they are identical, the output will be: The strings are equal.

Method 2: Case-Insensitive Comparison

If you want to ignore case differences between strings, you can use the casefold() method or convert both strings to lowercase or uppercase before comparing them.

Example Code:

string1 = "Hello"
string2 = "hELLo"

if string1.casefold() == string2.casefold():
    print("The strings are equal (ignoring case).")
else:
    print("The strings are not equal.")

In this example, the casefold() method is used to convert both strings to a case-insensitive format. Since they are identical in a case-insensitive manner, the output will be: The strings are equal (ignoring case).

Method 3: Sub-String Matching

You can use the in operator or the str.find() method to check if one string contains another.

Example Code:

string1 = "Hello World"
string2 = "World"

if string2 in string1:
    print("The string '{}' is contained in '{}'.".format(string2, string1))
else:
    print("The string '{}' is not contained in '{}.'".format(string2, string1))

In this example, the in operator is used to check if the string "World" is contained in the string "Hello World". Since it is, the output will be: The string 'World' is contained in 'Hello World'.

Conclusion

Comparing strings is a fundamental operation in Python programming that can be used in various scenarios. In this article, we explored three methods for comparing strings: exact match, case-insensitive comparison, and sub-string matching. By applying these concepts to your own projects, you will become more proficient in working with strings in Python.


Additional Resources

Note: The code examples provided are for illustration purposes only and may not be used in production code without proper error handling and testing.

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp