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!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨