How to Solve Python AttributeError: ‘str’ object has no attribute ‘trim’

by | Programming, Python, Tips

This error occurs when you try to call the trim() method on a string to remove whitespace. You can solve the error by using the strip() method to remove leading and trailing whitespace from the string. For example,

my_str = '  Python  '

clean_str = my_str.strip()

This tutorial will go through the error in detail and how to solve it with code examples.


AttributeError: ‘str’ object has no attribute ‘trim’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘str’ object has no attribute ‘trim’” tells us that the string object we handle does not have the attribute trim.

We can verify if an attribute exists for an object using the dir() function. For example,

my_str = '  Python '

print(type(my_str))

print('trim' in dir(my_str))
<class 'str'>
False

We can see that trim() is not in the list of attributes for the str object.

Example

Let’s look at an example of trying to call the trim() method on a list.

# Create string with whitespace

my_str = '  Research  '

# Attempt to remove white space using trim

clean_str = my_str.trim()

print(clean_str)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [8], in <cell line: 7>()
      3 my_str = '  Research  '
      5 # Attempt to remove white space using trim
----> 7 clean_str = my_str.trim()
      9 print(clean_str)

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

The error occurs because trim() is not a string method in Python.

Solution

We can solve the error by calling the str.strip() method that returns a copy of the string with leading and trailing whitespace removed.

# Create string with whitespace

my_str = '  Research  '

# Attempt to remove white space using trim

clean_str = my_str.strip()

print(clean_str)

Note that strings are immutable in Python, so the original string remains unchanged. We have to assign the new string to a variable. Let’s run the code to get the result.

Research

There are two other methods for stripping whitespace:

  • lstrip() removes any leading whitespace from a string
  • rstrip() removes any trailing whitespace from a string

Let’s look at an example of using both lstrip() and rstrip() on a string with both leading and trailing whitespace.

# Create string with whitespace

my_str = '  Research  '

# Attempt to remove white space using trim

lstripped_str  = my_str.lstrip()

print(lstripped_str)

rstripped_str = my_str.rstrip()

print(rstripped_str)
Research  
  Research

Summary

Congratulations on reading to the end of this tutorial!

For further reading on AttributeErrors with string objects, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!