Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Types and Branching String Methods

Gautam Basu
Gautam Basu
206 Points

Why cannot I write str_var.len()?

In your tutorial you have mentioned that everything in Python is Object , so in order to get some ability of the Object we should use . and then the method name with parenthesis.

If that is true then when I tried to execute str_var.len() instead of len(str_var), why did I get the following error?

quote.len() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'str' object has no attribute 'len'

2 Answers

Philipp Rรคse
PLUS
Philipp Rรคse
Courses Plus Student 10,073 Points

len() is a Built-In Python Function

but things like my_string.add() are methods of the String-Class.

The difference between methods and functions are explained in later videos.

Craig Dennis shows the documentation of the String-Class and the relating methods, mentioned inside the documentation. He skips it to avoid confusion on such an early stage of getting in touch with Python.

len(s)

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

I hope my answer helps you. :)

Sincerely,

Phil

For further information please follow the link above to the Python documentation.