1

This is the code which makes upper work:

a = 'Hello World'
print(a.upper())

And this is the code which I think is correct but doesn't work:

a = 'Hello World'
print(a.upper)

I'm using PyCharm if it helps

Note: I started learning Python today only, so please don't be very harsh.

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
SIddharth
  • 13
  • 5
  • since `upper` is a function you need to do a function call using `upper()` – balderman Sep 14 '21 at 18:24
  • 1
    `foo()` means, execute the function `foo` and pass no arguments to it. If you don't add the parentheses, Python won't automatically know to execute that function, so if you do something like `bar = foo` instead of `bar = foo()`, `bar` will point to the function itself rather than the value the function returned. – Random Davis Sep 14 '21 at 18:24
  • Yes, you do. That's what the error is telling you. `a.upper()` will call your string's `.upper()` function. `a.upper` will look for a property called `upper` on your string, fail to find such a thing, and throw an error. – Adam Smooch Sep 14 '21 at 18:25
  • @RandomDavis That should just be the answer. – Jab Sep 14 '21 at 18:25
  • Does this answer your question? [Purpose of calling function without brackets python](https://stackoverflow.com/questions/21785933/purpose-of-calling-function-without-brackets-python) – Jab Sep 14 '21 at 18:27

5 Answers5

3

(re-posting my comment as an answer)

foo() means, execute the function foo and pass no arguments to it. If you don't add the parentheses, Python won't automatically know to execute that function, so if you do something like bar = foo instead of bar = foo(), bar will point to the function itself rather than the value the function returned when executed.

Random Davis
  • 6,662
  • 4
  • 14
  • 24
2

Methods/Functions must be called with parentheses (). Attributes/Properties can be retrieved without parentheses. And there is a weird third case which I'll get into later.

Consider this example

foo.bar()
foo.baz

In typical usage bar would be a function/method of foo. baz would be some kind of attribute/property/parameter of foo.

In your case, upper is actually a function/method and should normally called with parentheses (). Consider this interactive result

>>> a = 'Hello World'
>>> a
'Hello World'
>>> a.upper()
'HELLO WORLD'
>>> a.upper
<built-in method upper of str object at 0x000001EC70EFAD70>

When you call a.upper() it works as you might expect. But when you call a.upper it says you're getting a method. I make this mistake all of the time when I forget to put parentheses on a function call. I don't know how common it is for other people.

Coming back to a.upper. With that, you're getting the actual function/method instead of the return value of the function/method. There are some cases where it can be useful to pass a function to another class or pass a function to another function. You can also use this feature to make your own name for a function. Consider

>>> a = 'Hello World'
>>> alias = a.upper
>>> alias()
'HELLO WORLD'
>>> alias
<built-in method upper of str object at 0x000001EC70EFAFB0>

I've assigned a.upper to something new called alias. And when I call alias(), then it returns the upper case of a.

bfris
  • 5,272
  • 1
  • 20
  • 37
1

You can use as like this too because upper lower is a method and preset method end with parenthesis

a = "hello world".upper()
print(a)

print("hello world".upper())

a = "hello world"
print(a.upper())
Ehsan Rahi
  • 61
  • 7
0

upper is a method of string (https://docs.python.org/3/library/string.html)

When you call a method, you need to put the round braces, even if it does not take any arguments.

styku
  • 146
  • 6
0

upper is a function and functions must be used as function(), where the () delimits possible parameters to the function. Since the function doesn't use any parameters, we leave the parentheses empty.

YozNacks
  • 155
  • 8