But why?


#1



Strings & Console Output


If:

parrot = "Norwegian Blue"
print len(parrot)

works (it does)

then I would expect:

parrot = "Norwegian Blue"
print lower(parrot)

Should work the same. It doesn't. Why not?


Replace this line with your code.


#2

that is because .lower() is a method call, so it should be:

x = "the string we want to get to lowercase"
print x.lower()

#3

len() is a built in function in Python which takes an argument, whereas .lower() is a method (as stetim pointed out) call for Strings.


#4

I found an answer. Several folks have asked the exact same question. Here was the reply:

The first example, len() is a function that takes an object.

The second example is a string method, str.lower() that will only work on string objects.

In other words, the len() function is not dedicated to any particular class of objects. We can use it on strings and lists in the exact same way.…

But the lessons don't even begin to mention differences between functions or method calls before using them.


#5

If the lesson would, you might be able to handle functions at this point, but classes (methods are function inside classes) would be overwhelming

for now, you would just have to write it down, we will get to it later

that is a problem with many things you want to learn, it has to be done in steps, and sometimes certain things have to be accepted till you have enough knowledge to explain why certain things (which you took for granted until that point) are a certain why (like the difference between function and method calls)


#6

I get that; I do - baby steps. Just saying, the previous lesson has you do something one way, to do it differently in the next example, then maybe just a line or two would be appropriate...."this example uses a string method, as opposed to the previous example which was a function. We'll talk more about those later, but for now, just note how they are used differently". Something like that would have eased my frustration considerably.


#7

the lesson only demonstrate how to use .lower(), its difficult to find exactly the right text for a lesson, other people might get confused or distracted when such text is present


#8

Next lesson:
8. Now let's look at str(), which is a little less straightforward.
The str() method turns non-strings into strings!

str() is a method, and methods are called like x.str(), but no, in this case it is str(x), like a function format?

Is there a good way to keep these straight?


#9

Found the answer to my question in the next lesson:
"Methods that use dot notation only work with strings."

Still very confusing for this newbie.


#10

well, looking at the docs:

https://docs.python.org/2/library/functions.html#str

str() seems to be a tricky one, because data types are classes, its super confusing. So normally a method is indeed x.methodName(), but when converting (int(), float(), str() you sort of ave exceptions:

But this advanced stuff, nice isn't it? (that is sarcastic)

Programming is very difficult, i am afraid that for now you have to keep your own head down until you actually get to classes, then we can delve into the deep :slight_smile:


#11

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