콘텐츠로 건너뛰기

파이썬에서 서브스트링 비교하는 방법은 무엇인가요?

CodeMDD.io

how to compare substring in python

How to Confirm That a Python String Contains Another String

To determine whether a string contains a substring in Python, you can use the membership operator in. This is the recommended way to check for the existence of a substring:

raw_file_content = """Hi there and welcome.
This is a special hidden file with a SECRET secret.
I don't want to tell you The Secret,
but I do want to secretly tell you that I have one."""
if "secret" in raw_file_content:
print("Found!")

The output will be:

Found!

If you want to check whether the substring is not in the string, you can use the not in operator:

if "secret" not in raw_file_content:
print("Not found!")

The output will be:

Not found!

Using the in operator returns a Boolean value (True or False) depending on whether the substring is found in the string.

Generalize Your Check by Removing Case Sensitivity

If you want to make your substring comparison case insensitive, you can convert both the string and the substring to lowercase or uppercase using the lower() or upper() methods. This way, you can compare them without worrying about the case:

raw_file_content = """Hi there and welcome.
This is a special hidden file with a SECRET secret.
I don't want to tell you The Secret,
but I do want to secretly tell you that I have one."""
if "secret" in raw_file_content.lower():
print("Found!")

The output will still be:

Found!

By converting both the string and the substring to lowercase using the lower() method, you ensure that the comparison is case insensitive.

Learn More About the Substring

To get more information about the substring, you can use the split() method. This method splits the string into a list of substrings based on a specified delimiter. By splitting the string, you can access each individual word or element and perform further checks:

raw_file_content = """Hi there and welcome.
This is a special hidden file with a SECRET secret.
I don't want to tell you The Secret,
but I do want to secretly tell you that I have one."""
words = raw_file_content.split()
if "secret" in words:
print("Found!")

The output will be:

Found!

In this example, the split() method is used to split the raw_file_content into a list of words. The membership operator is then used to check if the substring “secret” is present in the list.

Find a Substring With Conditions Using Regex

If you need to find a substring with specific conditions, you can use regular expressions (regex). The re module in Python provides functions for working with regular expressions. Here’s an example of how to find a substring using regex:

import re
raw_file_content = """Hi there and welcome.
This is a special hidden file with a SECRET secret.
I don't want to tell you The Secret,
but I do want to secretly tell you that I have one."""
if re.search(r"\bsecret\b", raw_file_content, re.IGNORECASE):
print("Found!")

The output will be:

Found!

In this example, the re.search() function is used to search for the substring “secret” with the \b metacharacters representing word boundaries. The re.IGNORECASE flag is used to make the search case insensitive.

Find a Substring in a pandas DataFrame Column

If you’re working with tabular data and need to search for substrings in pandas DataFrame columns, you can use the .str.contains() method. This method allows you to check if a substring is present in a column:

import pandas as pd
data = {
'Name': ['John', 'Jane', 'Mike', 'Sarah'],
'Age': [25, 30, 35, 40],
'Address': ['123 Main St', '456 Park Ave', '789 Walnut Dr', '321 Elm Ln']
}
df = pd.DataFrame(data)
substring = 'Park'
filtered_df = df[df['Address'].str.contains(substring)]
print(filtered_df)

The output will be:

Name Age Address
1 Jane 30 456 Park Ave

In this example, a DataFrame is created with columns for Name, Age, and Address. The str.contains() method is then used to filter the DataFrame and retrieve only the rows where the Address column contains the substring “Park”.

Key Takeaways

Comparing substrings in Python can be done using the in operator. To make the comparison case insensitive, you can convert the string and the substring to lowercase or uppercase. The split() method can be used to access individual words or elements of the string. Regular expressions can be used to find substrings with specific conditions. In pandas, the .str.contains() method can be used to search for substrings in DataFrame columns.

Now that you know how to compare substrings in Python, you can efficiently check for the presence of substrings in strings and perform actions based on the results.