1

Just starting out with Python

Could anyone explain the reasoning behind why some built in string functions take arguments within their brackets like this:

length = len("This is a string")

whilst some other functions seem to just be "chained" on to the end of the string that they are operating on for example:

uppercase = "lowercase string".upper()

Probably a silly question but just wondered if there was a reason behind this that I'm unaware of.

tommyd456
  • 10,443
  • 26
  • 89
  • 163
  • 1
    Related: [Is there a reason Python strings don't have a string length method?](http://stackoverflow.com/q/237128/464709) – Frédéric Hamidi Oct 04 '13 at 09:39
  • 1
    This is already answered here http://stackoverflow.com/questions/237128/is-there-a-reason-python-strings-dont-have-a-string-length-method – ecline6 Oct 04 '13 at 09:42
  • Also related (especially for the links and quotes in the question): [In Python when should I use a function instead of a method](http://stackoverflow.com/questions/8108688/in-python-when-should-i-use-a-function-instead-of-a-method) – Blckknght Oct 04 '13 at 09:59
  • apologies for the repeat question! – tommyd456 Oct 04 '13 at 13:13

4 Answers4

2

len() is a built in function, it returns length of a sequence, that is you can pass lists, tuples to len() not only strings. For example:

foo = (1,2,3)
len(foo)
>>> 3

bar = ['a', 'b', 'c', 'd']
len(bar)
>>> 4

And about brackets - in your example what is between brackets is a string. You can also do this:

foo = "This is a string"
len(foo)

And the

"lowercase string".upper()

Is calling a method of a string object, which returns uppercase of that string. You can do the same with:

foo = "lowercase string"
uppercase = foo.upper()
0

the function len() is a buildin-function in the language. len() docs

and the .upper() function that your using is part of the string class. str.upper() docs

Morrwo
  • 1
0

In the first case len is a function which you're calling within the current namespace, and you're passing a parameter to it. In the second, calling xyz.upper() means you are calling the method .upper() on the String object "lowercase string".

The len() function calls the __len__() method on the object passed in to it, so really it is just a shortcut for calling that method. Some other users have already posted links to the reasoning behind this (thank you for the correction larsmans).

melipone
  • 36
  • 3
0

Len is a function, a built-in function, so using len (something) you apply a transformation to your input and obtain an output, something like Y=f(X).

"some string".upper() is a Method of the instance "some string"; "some string" belongs to the class String and has its methods. Pay attention that "some string" written like that is an object and has all it's methods, you can see this if you type:

>>type("some string")
str

In summary: len is a function and is defined externally, .upper() is a method defined within the object itself

piertoni
  • 1,933
  • 1
  • 18
  • 30