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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The Python "AttributeError: 'str' object has no attribute 'contains'" occurs when we try to call the contains() method on a string. To solve the error, use the in operator, e.g. 'ab' in 'abc' as strings don't have a contains method.

attributeerror str object has no attribute contains

Here is an example of how the error occurs.

main.py
my_str = 'hello world' # ⛔️ AttributeError: 'str' object has no attribute 'contains' print(my_str.contains('world'))

We tried to call the contains() method on a string and got the error because strings don't have a contains method.

You can use the in operator to check if a substring is contained in a string.

main.py
my_str = 'hello world' if 'world' in my_str: print('substring is contained in string') # 👉️ this runs else: print('substring is NOT contained in string')

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

main.py
my_str = 'hello world' print('world' in my_str) # 👉️ True print('another' in my_str) # 👉️ False

If you need to check if a substring is contained in a string ignoring the case, convert both strings to lowercase.

main.py
my_str = 'HELLO WORLD' my_substr = 'woRLD' if my_substr.lower() in my_str.lower(): print('substring is in string') # 👉️ this runs else: print('substring is not in string')

We converted both of the strings to lowercase to be able to check if a substring is in a string ignoring the case.

If you need to check if a substring is NOT in a string, use the not in operator instead.

main.py
my_str = 'HELLO WORLD' if 'another' not in my_str: print('substring is NOT in string') # 👉️ this runs

x not in s returns the negation of x in s.

All built-in sequences and set types support the in and not in operators.

When used with a dictionary, the operators check for the existence of the specified key in the dict object.

Note that empty strings are always considered to be a substring of any other string.

main.py
my_str = 'hello world' print('' in my_str) # 👉️ True

A good way to start debugging is to print(dir(your_object)) and see what attributes a string has.

Here is an example of what printing the attributes of a string looks like.

main.py
my_string = 'hello world' # [ 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', # 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', # 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', # 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', # 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', # 'title', 'translate', 'upper', 'zfill'] print(dir(my_string))

If you pass a class to the dir() function, it returns a list of names of the class's attributes, and recursively of the attributes of its bases.

If you try to access any attribute that is not in this list, you would get the "AttributeError: str object has no attribute error".

Since contains() is not a method implemented by strings, the error is caused.

Conclusion #

The Python "AttributeError: 'str' object has no attribute 'contains'" occurs when we try to call the contains() method on a string. To solve the error, use the in operator, e.g. 'ab' in 'abc' as strings don't have a contains method.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.