Len() str() .upper() .lower()


#1

Ok so I don’t think i’m grasping when and when not to use len(string) str(object) or dot notation? Could some please elaborate a little on this for me?


#2

(very) specific to python:

The dot operator is for accessing attributes of an object (technically it can be made to do other things)

Objects are responsible for their own behaviour, such as looping or adding and the dot operator for that matter

Behaviour specific to a value type is therefore usually found as a method of an object, ultimately it’s just an organisation scheme.

Some methods are usually not used directly, such as the one used when looping. For those there’s syntax for using it (operators, for-loops etc), typically with some extra code for figuring out what the right thing to do is if there’s several places to look or some sanity checking or something…

str.upper has nothing to do with other value types, it’s a string method.

Several value types have some form of length. The len function is used to obtain that, but it still boils down to a method of such objects. length is a function which probably could have been removed and be accessed as a method only. Don’t know whether it’s there for historical and/or technical reasons. Doesn’t really matter. (One reason might be for the sake of reserving the name for it (__len__), and avoiding reserving a “normal” name like len, size, length because no such names are reserved for other reasons)

If something isn’t associated with a value type then it definitely isn’t a method.

Anyhow, you have no choice in the matter. If you try to use a name that doesn’t exist you’ll get a NameError. When you need something that you think probably already exists in the language you can’t start guessing and expect to find it that way (unless you have a good feel for where it may be, but then you’re not really blindly guessing any more) You also shouldn’t really ever be using something you don’t know how it behaves, meaning you gotta look up documentation or equivalent for that which will also tell you where that something is. Yes, programming involves a whole lot of googling.


#3

@capone703 I’ve been having trouble trying to completely understand the differences as well.
One thing I made a note of that I keep coming back to is; when using those methods that are taught within
Learning Python - Strings & Console Output, 9/16 Dot Notation, is this redesigned format of the instruction.

Capture

It’s one of the first notes I made, so I’m sorry it isn’t perfectly structured.
I tried to colorize and format; functions, strings, variables, and all attributes
that might make it easier to differentiate what’s used when and how.

Purple = A Function
Light Orange = String
Dark Orange = Variable
Blue = The Attribute
Brownish Red = Integer