How to Check if a String Contains a Substring in Python: Using the in Operator and Case-Insensitive Checks

When processing text data, you often need to check if a specific keyword is included. For example, looking for error messages in log files or checking for forbidden terms in user input.

In Python, using the in operator allows you to write this inclusion check very intuitively and concisely.

This article explains the basic usage of the in operator and how to perform checks without distinguishing between uppercase and lowercase letters.

目次

1. Basic Inclusion Check (in Operator)

To check if a specific string (substring) is contained within another string, use the in operator. The result is returned as a Boolean value (True or False).

Syntax:

result = "search_string" in target_string

Specific Usage Example: Log Message Analysis

Here is an example of checking whether the word “Failed” is included in a system log message.

# Server log message
log_message = "[2025-12-10 10:00:05] Connection to database Failed."

# Check if "Failed" is included
is_error = "Failed" in log_message

print(f"Target log: {log_message}")
print(f"Is 'Failed' included?: {is_error}")

if is_error:
    print("Error detected: Sending notification to administrator.")

Execution Result:

Target log: [2025-12-10 10:00:05] Connection to database Failed.
Is 'Failed' included?: True
Error detected: Sending notification to administrator.

As shown above, it is common to use it directly as a condition in an if statement.

Checking for Exclusion (not in)

Conversely, if you want to confirm that something is not included, use the not in operator.

email = "user-at-example.com"

# Check if "@" is NOT included
if "@" not in email:
    print("Invalid email address format.")

2. Case-Insensitive Checks

The in operator strictly distinguishes between uppercase and lowercase letters. Therefore, "python" in "Python Programming" returns False.

If you want to ignore case differences, such as in a search function, convert both strings to be compared using the .lower() (lowercase all) or .upper() (uppercase all) method before using the in operator.

Specific Usage Example: Tag Search

When searching for article category tags, this ensures a hit even if the user types “web” when the tag is “Web Development”.

# Article tags (Mixed case)
article_tags = "Technology, AI, Web Development, Python"

# Search keyword
search_keyword = "web"

print(f"Tag info: {article_tags}")
print(f"Search term: {search_keyword}")

# Direct comparison (Result is False)
result_case_sensitive = search_keyword in article_tags
print(f"Direct check: {result_case_sensitive}")

# Convert both to lowercase before comparison (Result is True)
# Looking for "web" inside "technology, ai, web development, python"
result_ignore_case = search_keyword.lower() in article_tags.lower()
print(f"Lowercase check: {result_ignore_case}")

Execution Result:

Tag info: Technology, AI, Web Development, Python
Search term: web
Direct check: False
Lowercase check: True

Summary

  • Use "part" in "whole" to determine if a string contains a substring.
  • Use not in to determine if a string is not included.
  • If you want to ignore uppercase and lowercase distinctions, convert both strings to lowercase with .lower() before checking.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次