Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Printing value after .upper

I am trying to print a string in all uppercase letters. When I run the print command it prints the type of x and the location.

Why does it print the operation instead of the result?

x = 'bacon'
x = x.upper
print x

>>> 
<built-in method upper of str object at 0x02A95F60>
like image 863
StmWtr Avatar asked Apr 16 '26 12:04

StmWtr


1 Answers

upper (and all other methods) is something like "function ready to use", that way you can do:

x = 'bacon'
x = x.upper
print x()

But the most common to see, and the way I think you want is:

x = 'bacon'
x = x.upper()
print x

"Ridiculous" example using lambda:

upper_case = lambda x: x.upper()
print(upper_case) # <function <lambda> at 0x7f2558a21938>
print(upper_case('bacon')) # BACON
like image 86
Miguel Avatar answered Apr 18 '26 00:04

Miguel