Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Tuples Multiple Return Values

Why is there () in string.upper, etc.?

Why don't string.upper work, and why do you need () at the end?

1 Answer

Christian Blume
Christian Blume
10,424 Points

Hello there,

upper() is a method of the class String. Methods are different from instance or class variables, since the former perform actions on your object (like calculating values, changing the state of your object etc.) and the latter hold information.

For example, in some languages String objects hold an instance variable called length describing the number of characters this string contains. You would get that value by calling length on your String object (note: there are no parenthesis (()) since length is an instance variable).

Then there are methods. These are always invoked by using the parenthesis. Some methods require you to provide arguments with which this method is supposed to do something, others don't. In cases where you need to provide arguments, these arguments are put in between the parenthesis, like in println("Hello, world!"). In this case, you pass the string "Hello, world!" as an argument to the method println() in order for the string to be printed to the console. The method upper() does not receive any arguments so nothing has to be put in between the parenthesis, but since you're are still calling a method the parenthesis have to be there.

I hope this clears things up for you.