-2

Why does python handle this

a = None
b = 'blub' 
print(a)
print(b)

but not

print(a + ' ' + b)

It gives: unsupported operand type(s) for +: 'NoneType' and 'str'

Why doesn't it just convert it to None as in the above case? And how could I get such behaviour?

Hakaishin
  • 2,550
  • 3
  • 27
  • 45
  • 1
    The error tells exactly what is not working: "unsupported operand type(s) for +". Issue is with plus operator not print. – Andrey Oct 25 '16 at 13:29
  • I understand the error and I know how to fix it. My question is why it is this way. – Hakaishin Oct 25 '16 at 13:30
  • 2
    @Hakaishin: because explicit is better than implicit. Convert your `None` value to a string first: `str(a) + ..`. Or use string formatting. – Martijn Pieters Oct 25 '16 at 13:31
  • In general, `+` does not try to guess whether `a + b` should convert `a` to `b`'s type, or convert `b` to `a`'s type. – chepner Oct 25 '16 at 13:33
  • @Hakaishin your question explicitly mentioned print while print has nothing to do with the issue. – Andrey Oct 25 '16 at 13:35
  • Very similar to recent question [here](http://stackoverflow.com/questions/40171202/python-integer-and-string-using) (can't mark as duplicate because there are no upvoted or accepted answers). – TigerhawkT3 Oct 25 '16 at 13:35
  • It has to do with print. Print decides to convert the None type to the string None or atleast that's what it prints. So I was wondering why doesn't + do the same. But I see it comes from the whole explicit is better the implicit. That reason together with @moses koledoye's answer would be the perfect answer I was looking for. A reason and another simpler way to achieve what I wanted. calling str(a) seems cumbersom, just adding the commas looks much better. – Hakaishin Oct 25 '16 at 13:37
  • Please use "string interpolation", there are several forms to choose from from `%s` to f-strings. – Dima Tisnek Oct 25 '16 at 16:32

3 Answers3

3

String concat was not designed to auto-coerce other types into strings on the fly, because explicit is better than implicit. You can use string formatting for that:

print('{} {} {}'.format(a, b, c))

Or just pass the parameters directly to print:

print(a, b, c)

You could also explicitly convert the other types to strings. And do the concat.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • Ah perfect, both solutions were what I was looking for. Thanks :) – Hakaishin Oct 25 '16 at 13:34
  • @StefanPochmann Yeah, just proof of concept. – Moses Koledoye Oct 25 '16 at 14:01
  • Had to wait a minimum amount of time :) – Hakaishin Oct 25 '16 at 14:45
  • Why did you remove the part "because 'the view that' explicit is better than implicit" ? That's definitely not true. I mean why would there even be such a thing like implicit conversion if it's never better? Implicit things have their fair use and claiming explicit is better than implicit is just wrong. @MosesKoledoye – Hakaishin Oct 25 '16 at 15:07
  • 1
    @Hakaishin I didn't remove it. The statement was taken from the [Zen of Python](https://www.python.org/dev/peps/pep-0020/) – Moses Koledoye Oct 25 '16 at 15:10
2

Because a and c are not strings, you should try :

print(str(a) + ' ' + b + ' ' + str(c))

The problem isn't "print", but the concatenation of the variables.

julien2313
  • 339
  • 2
  • 6
  • 22
1

As to why, look at the case of 36 + 'abc' Should this convert 36 to a string and concatenate the strings, or should it convert abc to a numeric value and add the numbers? There's no right answer, and so Python doesn't guess unless it has specific rules about those two types.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • To me it seems obvious it should be converted to a string. Like 90% of the time I don't want the string as a numeric value. And in the rare case I want it as a numeric value I will be really aware of it and convert it explicitly. To me it makes more sense Implicit for stuff that happens often explicit for rare cases. – Hakaishin Oct 25 '16 at 13:48
  • @Hakaishin - Why should it be converted to a string? If `36 + 'abc'` is implicitly converted, it should obviously evaluate to 2784. :P In the face of ambiguity like this, Python prefers not to guess. – TigerhawkT3 Oct 25 '16 at 13:53
  • As I explained because that is what people want in 90% of the cases. I understand that you can't formally prefer one over the other, but there is a real world where real people do things. This is the same kind of weird pythonism that prevents i++ being in python. – Hakaishin Oct 25 '16 at 15:10
  • I think `36 + '13'` would be a stronger example. Perl for example actually does evaluate that to 49. – Stefan Pochmann Oct 25 '16 at 16:00