Why can we not lower(var)


#1

Please leave the following link in the post so we can find the unit module, else replace it with a link to the exact lesson:

Strings and Console Output

Why can we put the variable in the method here:
print len(test)
4

But not to get rid of capitalization:
“Test”.lower()
‘test’

The following does not work, but why?
t = “Test”
print lower(t)

EDIT: Fixed Variable


#2

To call an object (such as a function) you first have to get a reference to that object (which you do by typing a name referring to it, like len). A whole lot of stuff is already defined when you start python, and more so if you import other modules.
But if you try to use a name that isn’t defined… Well, you’ll be told that’s not possible.

As for why len exists at module level and why lower is a string attribute, that’s because lower is string behaviour while len operates on anything that defines a length


#3
t = "Test"
print lower(test)

should be

t = "Test"
print t.lower()

the test variable doesnt exist


#4

My apoligies. I mean
print lower(t)

In terminal I get:
NameError: name ‘lower’ is not defined


#5

the syntax of the lower method on python is:

str.lower()

not

lower(str)

so you would want to try:

print t.lower()

#6

To be a bit picky, it’s not syntax. They’re both valid syntax, but one of them hasn’t been defined

It’s like saying:

print a + b

Without first defining a/b


#7

I understand that, I was just trying to understand the thinking behind this. I am new to Python so the () throws me off a bit.


#8

str.lower() is a string method while len() is a function like print()


#9

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