5

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
0

6 Answers 6

11

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
Sign up to request clarification or add additional context in comments.

Comments

6

Note that print is a function in Python 3. In Python 2, your first line would concatenate "Type string: " and "123" and then print them. In Python 3, you are calling the print function with one argument, which returns None, and then add "123" to it. That doesn't make any sense.

The second line doesn't generate an error in Python 2 or 3 (I've tested it with 2.7.7 and 3.2.3). In Python 2, you get

Concatenate strings and ints 10

while in Python 3, your script should only print

Concatenate strings and ints

This is because again, print is a function, and therefore you call it with the argument "Concatenate strings and ints". The , 10 makes your line a tuple of the return value of print, which is None, and 10. Since you don't use that tuple for anything, there is no visible effect.

2 Comments

Thank you for your response. I started learning with Python 2.7 yesterday and decided to just start with the latest version today.
Yeah, there are some differences between Python 2 and 3, which is the reason that Python 2 is still used today. The Python wiki has a page on the subject. For learning purposes, it doesn't really matter. Just stick to one version. If you need Python 2 for a specific project immediately, use Python 2 learning material, otherwise, stick to Python 3. Once you get the language, you can easily learn the differences between the two versions if needed.
3

Try format():

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

3 Comments

Really? Is this syntax specific to Python v3.4? Much more convenient in 2.7
This syntax is also present in 2.7. Old way to do it is print("Hello %s" % "world")
This is usually a better alternative to either concatenation or multiple print arguments, partly because it avoids the possibility of the asker's problem coming up… but without an explanation as to why it's better, it's not much of an answer here.
2

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.

Comments

1

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}")

1 Comment

An explanation would be in order.
-1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.