파이썬에서 서브스트링 비교하는 방법은 무엇인가요?
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:
The output will be:
If you want to check whether the substring is not in the string, you can use the not in
operator:
The output will be:
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:
The output will still be:
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:
The output will be:
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:
The output will be:
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:
The output will be:
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.