How to fix the attributeerror ‘str’ object has no attribute ‘contains’?

What is the attributeerror ‘str’ object has no attribute ‘contains’ error?

This error happens when you try to check whether a substring exists in a Python string using the str.contains() method which exists in the pandas library but in in standard python. Use instead the following syntax to check for a substring:

if sub_str in a_string:
    print('Substring found in string')
else:
    print ('Substring not found')

Reproducing the no attribute contains for strings error

You can easily reproduce the attribute error message. Feel fee to try it yourself using your favorite Python development environment: PyCharm, Jupyter, VsCode, Spyder etc’:

lang_str = 'Python and R are the most popular programming languages for Data Analysis.'

sub_str = 'Data'

lang_str.contains(sub_string)

This will render the error message:

AttributeError: 'str' object has no attribute 'contains'

Fixing the error

You can easily fix this error using the in operator, which we can use here in order to check whether a string is found in another:

if sub_str in lang_str:
    
   print ('Found')
else:
    print('Not found')

You can also use the str.find() function. This function return the character position in which the sub-string is found:

if lang_str.find(sub_str) > -1:
   print ('Found')
else:
    print('Not found')

Using str.contains() in pandas

The str accessor is very helpful as it provides a simple way to perform string related operators such as capitalization, splitting and so forth. on Pandas DataFrame columns (Series objects).

Let’s see a very simple example. Note that you will need to import the pandas Data Analysis library if it’s not yet installed in your Python development environment.

# importing the pandas library
import pandas as pd

# define a pandas Series
my_s = pd.Series(['Java', 'Python', 'R', 'Julia', 'Go'])
sub_str = 'Python'

#check if the Series elements include the sub-string we defined
if (my_s.str.contains(sub_str).any()):
    print ('Substring found in Series')
else:
    print ('Substring NOT found in Series')

This will return the following:

Substring found in Series