Hey, I have previously programmed in C, PHP & Javascript and I have recently started out with Python but the syntax of the functions is quite different and confusing? Like if we call len(abc) we enter the param inside the function unlike the function abc.upper() where we use enter the param using a dot. Its kinda confusing?

Recommended Answers

All 5 Replies

The dot operator is used to acces methodes and variables that are part of a class. It's saying from class abc i'm using the upper function. This function still takes it's parameters in between the brackets. You are just saying that you want to use the upper function from the abc class and not some random other upper() function.

>>> 'abc'.upper()
'ABC'
commented: yes, but why do we enter the argument using the dot. Why not inside the brackets? +0

if we want to convert 'xyz' to uppercase, why dont we enter the argument inside the function. I means 'xyz' string is not a class?

a string is an object of the class String. which is why we can use the function on it.

if we want to convert 'xyz' to uppercase, why dont we enter the argument inside the function

Because uppercase() is a method that belong to string class and only works on strings.
The Built-in Functions work on many class/objects.
Let's see how len() work on string and list.

>>> s = 'hello'
>>> len(s)
5

>>> lst = [1,2,3]
>>> len(lst)
3

Both string and list has methods that belong only to string and list class.
Look at append() for list.

>>> lst = [1,2,3]
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]

append() work only for list,so no point that it should built in function.

s = 'hello'
>>> s.append('world')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'

Use dir(str) or dir(list) to see methods that belong to string and list class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.