-2

Hi there I am trying to create variables and then print each variable on a seperate line without printing each variable seperately like this:

string_name = "Pierre"
integer_age = 36
float_height_in_metres = 1.94
print string_name 
print integer_age
print float_height_in_metres

I would like to only use the print command once. I tried this, but an error occurs when trying to run.

string_name = "Pierre"
integer_age = 36
float_height_in_metres = 1.94
print string_name + \ninteger_age + n\float_height_in_metres

Is it possible to only use the print command once?

1
  • What error occurs ?
    – Idos
    Commented Jun 6, 2018 at 17:57

1 Answer 1

0

Your code fails because + concatenates strings only, but you pass a string, an integer and a float. The error you should get is pretty clear about this:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

You have to convert each to their string representation first:

print string_name + '\n' + str(integer_age) + '\n' + str(float_height_in_metres)
1
  • Thank you I am still learning, so what you are saying is that I have to cast each integer and float into a string before I can print them using + Commented Jun 6, 2018 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.