Link to home
Create AccountLog in
Avatar of john8217
john8217

asked on

Python, can I concatenate a string and an int?

I started learning Python. I created this simple code which involves concatenating a string and an integer. I just found out that Python doesn't like that. Here is the code I'm typing. Python gave me an error message because of line 4.
     character_name = "John"
     character_age = 35
     print("There was a man named " + character_name + ".")
     print("He was " + character_age + " years old")
     character_name = "Mike"
     print("There was someone else named " + character_name + ".")
     print("But he didn't like being 35")
How do I concatenate a string an in int
Avatar of gelonida
gelonida
Flag of France image

You cannot concatenate a string and an integer.

Howver you can convert the integer to a string to concatenate or you can use string formatting.

There's two different kinds of string formatting.
The 'old style formatting', that's more similiar to the printf formatting style of the C language
and the new formatting style which is more powerful, but a little more to type for simple cases.

Old style with the '%' operator

New style formatting with the .format() method

Documentation for new style formatting:
https://docs.python.org/3/library/string.html?highlight=string#string.ascii_uppercase
and examples at
https://docs.python.org/3/library/string.html?highlight=string#formatexamples

Documentation for old style formatting:
https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting

      character_name = "John"
     character_age = 35
     print("There was a man named " + character_name + ".")
     print("There was a man named %s." % character_name)
      print("There was a man named {}.".format(character_name))
     print("He was " + str(character_age) + " years old")
     print("He was %d years old" % character_age)
     print("He was {} years old".format(character_age))
     print("The man %s was %d years old" % (character_name, character_age))
     print("The man {} was {} years old".format(character_name, character_age))
     character_name = "Mike"
     print("There was someone else named " + character_name + ".")
     print("But he didn't like being 35")

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
@pepr: True.
the new formatted string literal is the third way of formatting which I forgot to mention

even the old style % operator supports map formatting.
So if you have a dict instead of separate viarables, then it's also possible to do.

info = { "name":  "John", "age": 35}

# old style
print("The name is %(name)s and the age is %(age)d" % info)

# new style
print("The name is {name} and the age is {age}".format_map(info))

Open in new window

Avatar of john8217
john8217

ASKER

Thank you both.
Geez, I didn't know that something so seemingly simple as concatenation could be so involved.
Also it can be written in short like this...
>>> character_name = 'john'
>>> character_age = 35
>>> 'The man {} was {} years old'.format(character_name,character_age)
'The man john was 35 years old'

Open in new window

Each {} in the string is a placeholder which can be replaced with it's corresponding variable within .format()
@Sudobh: see line 10 of my first answer
Yes, now I see that you already showed that example. Sorry I didn't see it.