-1

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.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Ayush Sharma
  • 19
  • 1
  • 2
  • 5

3 Answers3

1

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.

xandermonkey
  • 4,054
  • 2
  • 31
  • 53
0

Try formatting strings:

print( f'{string} {age}' )

Oleg Butuzov
  • 4,795
  • 2
  • 24
  • 33
0

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
Nathan White
  • 134
  • 6