0

I am creating a practice program that displays the name of a character along with his age. For some reason, whenever I store the character's age without putting quotes around it, it will not print.

I saw in a video that when storing numbers as a variable, you do not need to put quotes around it. Can you guys please examine my code to let me know what I need to change or add.

character_name = "Tyrone"
character_age = 22
print("There was once a man named " + character_name + ",")
print("he was " + character_age + " years old.")
print("He really liked the name " + character_name + ",")
print("but didn't like being " + character_age + ".")

TypeError: can only concatenate str (not "int") to str

africanxmamba
  • 91
  • 3
  • 13
  • Or use `"There was once a man named {},".format(character_age)` - you can't concatenate int value, so either convert it with `str(character_age)`, or use `format`. – dmitryro Aug 06 '18 at 23:49

3 Answers3

4

there are many solutions:

  • Pass the number as a separate parameter to print() - the print() function can take multiple parameters:

    print("he was", character_age, "years old.")
    
  • Use formatting to create a string with the number in it:

    print("he was %s years old." % character_age)
    print("he was {} years old.".format(character_age))
    print(f"he was {character_age} years old.")
    
  • convert the number to a string before concatenation:

    print("he was " + str(character_age) + " years old.")
    
nosklo
  • 217,122
  • 57
  • 293
  • 297
0

The best way is to use format() and the symbol {} in your strings.

It's also more portable, and works with all kinds of objects, including numbers and strings.

And it is way clearer, your text message keeps entirely there, look at that!

print("There was once a man named {},".format(character_name))
print("he was {} years old.".format(character_age))
print("He really liked the name {},".format(character_name))
print("but didn't like being {}.".format(character_age))
rsalmei
  • 3,085
  • 1
  • 14
  • 15
-1

For python <2.7 and 3 you can use:

#!/bin/env python
character_name = "Tyrone"
character_age = 22
print("There was once a man named " + character_name + ",")
print("he was {0}".format(character_age) + " years old.")
print("He really liked the name " + character_name + ",")
print("but didn't like being {0}".format(character_age) + ".")

Optionally for python >2.7 you can exclude the position qualifier - {} instead of {0} will work.

bgercken
  • 1
  • 4
  • Python 2.7 also has `{}`. – abarnert Aug 07 '18 at 00:15
  • Ah yes - I actually tested it on python 2.66 and it did not work: ```Python 2.6.6 (r266:84292, Sep 4 2013, 07:46:00) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> i=10 >>> print("i: {}".format(i)) Traceback (most recent call last): File "", line 1, in ValueError: zero length field name in format >>> print("i: {0}".format(i)) i: 10 >>>``` – bgercken Aug 07 '18 at 11:27