I have written the following script:
string="My name"
age=19
print(string +str(age))
But this gives an error stating:
"str object is not callable".
Help me to typecast and print.
I have written the following script:
string="My name"
age=19
print(string +str(age))
But this gives an error stating:
"str object is not callable".
Help me to typecast and print.
Like mentioned in the comments, it looks like you have re-assigned the built in function str
to something else. Find and remove that, and your code should work.
It's also worth noting that in python3
the print()
function will attempt to cast your arguments to strings if you separate them with a ,
. As seen here:
print(string, age)
Will cast age
to a string for you.
Try formatting strings:
print( f'{string} {age}' )
String.format can help here.
print ("{0} {1}".format(string, age) )
this results is the following being printed.
My name 19
You can use this method to improve the readability of the output as well.
print ("Name: {0} age: {1}".format(string, age)
This would result in the following output.
Name: My Name age: 19