'Concatenate string and int in Python 3 .4 [duplicate]

I'm new to Python, so I've been running through my own set of exercises to simply start memorizing basic functions and syntax.

I'm using the PyCharm IDE and Python 3.4. I've run into an issue when running through some basic string and integer concatenation exercises. Each instance below is throwing an unsupported operand type. There are several threads on Stack Overflow that clearly states proper concatenation syntax, but the above error message continues to plague me.

print ("Type string: ") + str(123)
print ("Concatenate strings and ints "), 10


Solution 1:[1]

In Python 3+, print is a function, so it must be called with its arguments between parentheses. So looking at your example:

print ("Type string: ") + str(123)

It's actually the same as:

var = print("Type string: ")
var + str(123)

Since print returns nothing (in Python, this means None), this is the equivalent of:

None + str(123)

which evidently will give an error.

That being said about what you tried to do, what you want do to is very easy: pass the print function what you mean to print, which can be done in various ways:

print ("Type string: " + str(123))

# Using format method to generate a string with the desired contents
print ("Type string: {}".format(123)) 

# Using Python3's implicit concatenation of its arguments, does not work the same in Python2:
print ("Type string:", str(123)) # Notice this will insert a space between the parameters

Solution 2:[2]

Try format():

print("Type string: {}".format(123))
print("Concatenate strings and ints {}".format(10))

Solution 3:[3]

There is nothing wrong with this:

print ("Type string: ") + str(123)

print is just a function like anything else. And you're calling that function with one argument, "Type string: ", and then trying to add the result (which will be None) to the string '123'. That isn't going to work. If you wanted to add the two strings together, you have to put them into the same expression, inside the parentheses:

print("Type string: " + str(123))

Similarly:

print ("Concatenate strings and ints "), 10

This calls print with one argument, and then makes a tuple of the None returned by print and the number 10. If you want to pass 10 to the print call, it has to go inside the parentheses:

print("Concatenate strings and ints ", 10)

As gitaarik's answer points out, using str.format is more flexible, and avoids the possibility of problems like this. It also gives you code that works exactly the same way in both Python 2.6-2.7 and Python 3.x, which is pretty nice even if you aren't trying to write dual-platform/single-codebase code, because it'll be understandable even to people who only know one or the other.

Solution 4:[4]

I think this is a pretty cool way to concatenate a string and an int in Python:

print (f"Type string: {123}")
print (f"Concatenate strings and ints {10}")

Solution 5:[5]

You can do it like this:

c = 'Emerson'
d = 32
print("My name is %s and I am %d years old." %(c,d))

Result:

My name is Emerson and I am 32 years old.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 gitaarik
Solution 3 Peter Mortensen
Solution 4 Peter Mortensen
Solution 5 SnowBG