We help IT Professionals succeed at work.

Python, can I concatenate a string and an int?

john8217
john8217 asked
on
134 Views
Last Modified: 2019-02-18
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
Comment
Watch Question

CERTIFIED EXPERT

Commented:
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

CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
@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

Author

Commented:
Thank you both.
Geez, I didn't know that something so seemingly simple as concatenation could be so involved.
Subodh Tiwari (Neeraj)Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015

Commented:
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()
CERTIFIED EXPERT

Commented:
@Sudobh: see line 10 of my first answer
Subodh Tiwari (Neeraj)Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015

Commented:
Yes, now I see that you already showed that example. Sorry I didn't see it.