Open In App

Pandas Series.str.contains() - Python

Last Updated : 12 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Series.str.contains() method in Pandas allows us to check whether a given substring or regex pattern exists within each string in a Series or Index. This is especially useful when filtering or flagging text data based on specific patterns. For example, suppose we have a Series of city names and want to check which names contain the substring "is". This method will return a Boolean Series indicating True for matches and False otherwise.

Python
import pandas as pd

sr = pd.Series(['Paris', 'New York', 'Lisbon'])
print(sr.str.contains('is'))

Output
0     True
1    False
2     True
dtype: bool

In this case, "Paris" and "Lisbon" contain "is", so the output shows True for those entries.

Syntax

Series.str.contains(pat, case=True, flags=0, na=np.nan, regex=True)

Parameters:

  • pat: Substring or regex pattern to search for
  • case: Case-sensitive search if True (default)
  • flags: Regex flags from the re module, e.g., re.IGNORECASE
  • na: Value used for missing values
  • regex: If True, treats pat as a regex pattern

Returns: A Series or Index of boolean values indicating match results.

Example 1: Search for Substring

Let’s check if the substring "is" is present in each city name in a Series.

Python
import pandas as pd

sr = pd.Series(['New_York', 'Lisbon', 'Tokyo', 'Paris', 'Munich'])
sr.index = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']

res = sr.str.contains('is')
print(res)

Output

Search-for-Substring
Search for Substring


Explanation: "Lisbon" and "Paris" contain the substring "is", so True is returned for those cities. This is a basic use-case of substring matching.

Example 2: Use Regex to Match a Pattern

Now, let’s use a regular expression to match any string that has the letter "i" followed by a lowercase letter ([a-z]).

Python
import pandas as pd

sr = pd.Series(['Mike', 'Alessa', 'Nick', 'Kim', 'Britney'])
sr.index = ['Name 1', 'Name 2', 'Name 3', 'Name 4', 'Name 5']

res = sr.str.contains('i[a-z]', regex=True)
print(res)

Output

Regex-to-Match-a-Pattern
Use Regex to Match a Pattern


Explanation: "Mike", "Britney", "Nick" and "Kim" match the pattern "i[a-z]" (i followed by k and t respectively) and the rest do not match the condition.

Example 3: Case-Insensitive Match Using Flags

You can make the match case-insensitive using flags=re.IGNORECASE.

Python
import pandas as pd
import re

sr = pd.Series(['apple', 'Banana', 'Mango', 'berry', 'GRAPE'])
sr.index = ['Fruit 1', 'Fruit 2', 'Fruit 3', 'Fruit 4', 'Fruit 5']

res = sr.str.contains('grape', flags=re.IGNORECASE)
print(res)

Output

Case-Insensitive-Match-Using-Flags
Case-Insensitive Match Using Flags


Explanation: Even though "GRAPE" is in uppercase, it matches "grape" due to IGNORECASE flag. This is useful when text casing is inconsistent.


Next Article

Similar Reads