0
about str function
what is the use of str function in every operation of calculator for example: c=str(a-b) ---------- here the part of str...?
3 Answers
+ 3
It is not a function, it is a string class.
Calling str() will call string constructor and create new string instance.
str() creates a string format of a value.
This is very useful because it helps you to handle values as strings using their string formats, this can also be treated as type conversion to string:
str(8) + str(9) = "89"
int(str(135)[::-1]) = 531
You can similarly often also convert strings to other types, like integers.
There is a very similar function repr, which is often a little faster with almost same result.
repr focuses on what a value would look like as a string
str focuses on how the value would be converted to a string
+ 3
Seb TheS ,
I'm not a python expert but official documentation says str() is a function š
"in particular, practically all objects can be compared for equality, tested for truth value, and converted to a string (with the repr() function or the slightly different str() function)."
from 4th paragraph of this page :
https://docs.python.org/3/library/stdtypes.html
+ 3
š®š³Omkarš technically you are both right, str is a built-in class, so str() is a constructor that returns a string object.