why we use str() method in "str(number)" format to convert a number to a string why not to use "number.str()" format, what is the difference between the both, what is difference between methods and attributes
Asked
Active
Viewed 129 times
0
-
2Have you looked at [this post](https://stackoverflow.com/questions/41168899/does-python-str-function-call-str-function-of-a-class)? – Mihai Chelaru Nov 19 '18 at 04:07
1 Answers
0
In Python, there is a standard method obj.__str__()
that is called when you try to convert an object to a string. When you call str(number)
what it really does is calling the underlying __str__
method on the number obj.
__str__
is a magic method, and it is not a good practice to call them explicitly. Also using str(obj)
is shorter than obj.__str__()
and str(obj)
is standard way of converting an object to string in python.
Regarding number.str()
, there is no such method in python basic number types at the moment, so cannot really elaborate on the differences

Alex
- 591
- 1
- 6
- 13