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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
apt-get
  • 93
  • 1
  • 1
  • 3

6 Answers6

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
Rafael Lerm
  • 1,340
  • 7
  • 10
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Carsten
  • 17,991
  • 4
  • 48
  • 53
  • Thank you for your response. I started learning with Python 2.7 yesterday and decided to just start with the latest version today. – apt-get Sep 28 '14 at 22:37
  • 1
    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](https://wiki.python.org/moin/Python2orPython3) 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. – Carsten Sep 28 '14 at 22:45
3

Try format():

print("Type string: {}".format(123))
print("Concatenate strings and ints {}".format(10))
gitaarik
  • 42,736
  • 12
  • 98
  • 105
  • Really? Is this syntax specific to Python v3.4? Much more convenient in 2.7 – apt-get Sep 28 '14 at 22:23
  • This syntax is also present in 2.7. Old way to do it is `print("Hello %s" % "world")` – gitaarik Sep 28 '14 at 22:26
  • 1
    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. – abarnert Sep 28 '14 at 22:31
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
abarnert
  • 354,177
  • 51
  • 601
  • 671
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}")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sayalok
  • 882
  • 3
  • 15
  • 30
-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.

SnowBG
  • 89
  • 1
  • 2
  • 12