Len() vs. upper() and lower()


So I'm working on Strings and Console output for Python.

I have just learned about the "len()" command, and am now attempting to familiarize myself with the upper() command. I was asked to change all the letters in a string to uppercase, so I put the variable for my string in the parenthesis, as I did when finding the length of the string.

"print len(var)"seems to work, however "print upper(var)" does not.

"print var.upper()" does work however.

Why is this so? Also, if nothing is going in the parenthesis why must I include them?


upper() is a method whereas len() is a function.

Here i found something that might explain it a little better, it says:

Functions were used for those operations that were generic for a group of types and which were intended to work even for objects that didn’t have methods at all (e.g. tuples). It is also convenient to have a function that can readily be applied to an amorphous collection of objects when you use the functional features of Python (map(), apply() et al).

In fact, implementing len(), max(), min() as a built-in function is actually less code than implementing them as methods for each type. One can quibble about individual cases but it’s a part of Python, and it’s too late to make such fundamental changes now. The functions have to remain to avoid massive code breakage.

This just kind of explains why some are functions and some are methods. It can be extremely confusing when you start, because there are things like commands which are things like print, return, etc.
Python documentation, (which is what the hyperlink leads to), can explain some of this better.

I don't have an exact, clear answer, but it's the same as calling a function. I don't know if you've started functions yet, but when calling a function you've previously defined, you use the parenthesis even if you don't have an argument to pass it. It's just correct syntax.
I hope this is what you're looking for! :slight_smile:


Thanks for getting back to me so fast. That definitely helped, cheers!


No problem getting back to you quickly! It's a forum, so sometimes people do, and sometimes people don't. It helped that you had a real question that was easy to understand and figure out what you want to know.
Glad I could help some! :slight_smile:


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