Why is this giving me an error?
>>> variable = str(21)
Traceback (most recent call last):
File "<pyshell#101>", line 1, in <module>
variable = str(21)
TypeError: 'str' object is not callable
We can convert numbers to strings using the str() method. We'll pass either a number or a variable into the parentheses of the method and then that numeric value will be converted into a string value.
To convert an integer to string in Python, use the str() function. This function takes any data type and converts it into a string, including integers. Use the syntax print(str(INT)) to return the int as a str , or string.
Convert int to string python without str() function Simple example code keeps dividing a given int value by 10 and prepending the remainder to the output string. Use the ordinal number of '0' plus the remainder to obtain the ordinal number of the remainder, and then convert it to string using the chr function.
That code alone won't give you an error. For example, I just tried this:
~ $ python3.2
>>> variable = str(21)
>>> variable
'21'
Somewhere in your code you're defining that str =
something else, masking the builtin definition of str
. Remove that and your code will work fine.
Because you've probably overwritten the str
function by calling your own variable str
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With