In the previous lesson we used len() to print the number of characters in the string "Norwegian Blue" We formatted it this way

print len(parrot)

In this lesson we use

print parrot.lower()

Why do we put the variable in the brackets in the first example, and behind a dot in the second? It feels more intuitive to stick to one format, and use lower as

print lower(parrot)

I'm sure there is a specific reason that's why I'm asking here. Thanks for the help!


The first example, len() is a function that takes an object.

The second example is a string method, str.lower() that will only work on string objects.

In other words, the len() function is not dedicated to any particular class of objects. We can use it on strings and lists in the exact same way. str is a class of objects to which all strings belong. The method is a function of the class, and can only be accessed by string objects. lower() is said to be an attribute of the string object, as signified by the dot notation.

The concept of class will arise later in the course, so don't trouble yourself too much with studying this concept, just now. It is sufficient to know that all object in Python belong to a particular class. Strings are members of the str class; lists are members of the list class; dictionaries are members of the dict class; integers are members of the int class; floats are members of the float class, and so on. It will begin to make more sense as you progress.


Thank you very much for the quick reply. I figured it would be covered later in the course but with the way I put things together in my head while I'm learning I wanted to ask. I look forward to reflecting on this post when I learn more about classes later on!


You're very welcome. There is no restriction on us having the documentation near at hand, and the best time to read them is when new concepts surface. We don't need to bury ourselves in the docs; just get familiar with them so you can find your way around. Little by little your knowledge base will expand if you add to it slowly, while new concepts are fresh in your mind.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.