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 Check if String Contains Substring in Python

Learn how to check if a string contains a substring in Python using various methods, including the in operator, slicing, and regular expressions. …


Updated June 18, 2023

Learn how to check if a string contains a substring in Python using various methods, including the in operator, slicing, and regular expressions.

As a world-class expert in Python programming, I’m excited to share this article on how to check if a string contains a substring in Python. This fundamental concept is essential for any programmer working with strings in Python.

Definition of the Concept

The concept of checking if a string contains a substring refers to determining whether one sequence of characters (the substring) exists within another sequence of characters (the original string). In other words, we want to verify that a certain phrase or word is present within a larger text.

Step-by-Step Explanation

To check if a string contains a substring in Python, follow these steps:

Method 1: Using the in Operator

The simplest way to achieve this is by using the in operator. The in operator checks for membership of an element in a sequence (such as a list or tuple) or presence of a substring within another string.

# Define two strings
original_string = "Hello, World!"
substring = "World"

# Use the 'in' operator to check if the substring is present
if substring in original_string:
    print(f"The substring '{substring}' is present in the original string.")
else:
    print(f"The substring '{substring}' is not present in the original string.")

In this example, we define two strings: original_string and substring. We then use a conditional statement (if) to check if the substring is present within the original_string using the in operator.

Method 2: Using Slicing

Another way to achieve this is by using Python’s slicing feature. The find() method returns the index of the first occurrence of the substring, or -1 if not found.

# Define two strings
original_string = "Hello, World!"
substring = "World"

# Use the find() method to check if the substring is present
index = original_string.find(substring)

if index != -1:
    print(f"The substring '{substring}' is present in the original string at index {index}.")
else:
    print(f"The substring '{substring}' is not present in the original string.")

In this example, we use the find() method to locate the first occurrence of the substring within the original_string. If a match is found, we print a message indicating its location. Otherwise, we indicate that the substring is not present.

Method 3: Using Regular Expressions (Regex)

Finally, you can also achieve this using regular expressions with Python’s built-in re module. Here’s an example:

import re

# Define two strings
original_string = "Hello, World!"
substring = "World"

# Use a regex pattern to check if the substring is present
pattern = re.compile(re.escape(substring))
match = pattern.search(original_string)

if match:
    print(f"The substring '{substring}' is present in the original string.")
else:
    print(f"The substring '{substring}' is not present in the original string.")

In this example, we define a regex pattern using the re.compile() function. We then use the search() method to locate any occurrences of the pattern within the original_string. If a match is found, we indicate that the substring is present.

Conclusion

In this article, we explored how to check if a string contains a substring in Python using various methods: the in operator, slicing with the find() method, and regular expressions. Each approach has its strengths and weaknesses, but they all achieve the same goal of verifying whether one sequence of characters is present within another.

As a world-class expert in Python programming, I hope this comprehensive guide has provided you with valuable insights into how to check if a string contains a substring in Python.

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

Intuit Mailchimp