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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The Python "AttributeError: 'str' object has no attribute 'trim'" occurs when we try to call the trim() method on a string. To solve the error, use the strip() method to remove leading and trailing whitespace from the string.

attributeerror str object has no attribute trim

Here is an example of how the error occurs.

main.py
my_str = ' hello world ' # ⛔️ AttributeError: 'str' object has no attribute 'trim'. Did you mean: 'strip'? result = my_str.trim()

Strings don't have a trim() method in Python, however you can use the strip() method to remove any leading or trailing whitespace from the string.

main.py
my_str = ' hello world ' result = my_str.strip() print(result) # 👉️ "hello world"

The str.strip method returns a copy of the string with the leading and trailing whitespace removed.

The method does not change the original string, it returns a new string. Strings are immutable in Python.

Make sure to store the new string in a variable as the original string remains unchanged.

You can use the lstrip method to remove any leading whitespace from a string.

main.py
my_str = ' hello world ' result1 = my_str.lstrip() print(result1) # 👉️ "hello world "

The str.lstrip method returns a copy of the string with the leading whitespace removed.

Conversely, you can use the rstrip method to remove any trailing whitespace.

main.py
my_str = ' hello world ' result2 = my_str.rstrip() print(result2) # 👉️ " hello world"

The str.rstrip method returns a copy of the string with the trailing whitespace removed.

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 the str object doesn't implement a trim() method, the error is caused.

Conclusion #

The Python "AttributeError: 'str' object has no attribute 'trim'" occurs when we try to call the trim() method on a string. To solve the error, use the strip() method to remove leading and trailing whitespace from the string.

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.