2

I was writing a simple Hello World Python script and tried a couple different things to see what I would get.

Results:

print "Hello World"      ===> Hello World
print "Hello"," ","World ===> Hello   World
print "Hello" "World"    ===> HelloWorld

The results surprised me... from experience with other languages I expected to get something more like this:

print "Hello World"      ===> Hello World
print "Hello"," ","World ===> Hello World
print "Hello" "World"    ===> Syntax Error!

After trying a couple more examples I realized that it seems to add a space whenever you separate strings with a ",".

...Even more strangely, it doesn't seem to care if you give it multiple strings without a "," separating them as the third example shows.


Why does Python's print function act this way???

Also is there a way to stop it from adding spaces for "," separated strings?

tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
  • Python inserts a space between each element when you write with `print`. Concatenate the strings manually if you don't like it (or, with Python3, use `print(..., sep="")`) – Colonel Thirty Two Sep 16 '15 at 16:32
  • Why is this question being downvoted? There doesn't seem to be anything wrong with it. – TehTris Sep 16 '15 at 16:33
  • @ŁukaszR. Try `print "a", "b"` and `print ("a", "b")`. It's not the same. – Colonel Thirty Two Sep 16 '15 at 16:33
  • Very interesting! I will have to read up on Python syntax – tjwrona1992 Sep 16 '15 at 16:35
  • @ColonelThirtyTwo you are absolutely right. – Łukasz Rogalski Sep 16 '15 at 16:35
  • And I agree @TehTris, some people just like to downvote questions when the person asking it is new to the language. It creates a very negative environment ... but what can you do? – tjwrona1992 Sep 16 '15 at 16:36
  • Since you're already familiar with other programming languages, you should definitely read the official Python tutorial, as it's aimed at readers with some prior programming experience. – PM 2Ring Sep 16 '15 at 16:36
  • Thanks @PM2Ring, I will take a look at it – tjwrona1992 Sep 16 '15 at 16:37
  • It's possibly not downvoted because you are new, but because (as the downvote tooltip suggests) you didn't show any effort to research the answer for yourself. It might also be that the question is a bad fit for StackExchange which is intended to be direct question/answer pairs that people can find on Google, so people might be considering it "not useful" on those grounds. – TessellatingHeckler Sep 16 '15 at 16:48
  • I actually did attempt to do some research... Try asking Google why Python's print statement adds spaces. You get no useful information. There are also many locations on the internet where the Python print statement is discussed with no mention of any of this... Basic tutorials just tell you to type `print "Hello World"` and expect you to be happy with the result and move on with no level of detail. – tjwrona1992 Sep 16 '15 at 16:58

1 Answers1

6

Because the print statement adds spaces between separate values, as documented:

A space is written before each object is (converted and) written, unless the output system believes it is positioned at the beginning of a line.

However, "Hello" "World" is not two values; it is one string. Only whitespace between two string literals is ignored and those string literals are concatenated (by the parser):

>>> "Hello" "World"
"HelloWorld"

See the String literal concatenation section:

Multiple adjacent string literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation.

This makes it easier to combine different string literal styles (triple quoting and raw string literals and 'regular' string literals can all be used to create one value), as well as make creating a long string value easier to format:

long_string_value = (
    "This is the first chuck of a longer string, it fits within the "
    'limits of a "style guide" that sets a shorter line limit while '
    r'at the same time letting you use \n as literal text instead of '
    "escape sequences.\n")

This feature is in fact inherited from C, it is not a Python invention.

In Python 3, where print() is a function rather than a statement, you are given more control over how multiple arguments are handled. Separate arguments are delimited by the sep argument to the function, which defaults to a space.

In Python 2, you can get the same functionality by adding from __future__ import print_function to the top of your module. This disables the statement, making it possible to use the same function in Python 2 code.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Wow thanks Martijn, This is very informative. My python version is 2.7.8 so I may look into that `__future__` import you are talking about. Its also quite interesting that the Python parser automatically concatenates 2 strings separated by a space for you! – tjwrona1992 Sep 16 '15 at 16:39
  • Unfortunately I can't go to version 3 since it is incompatible with CVS and where I work they still use CVS as their version control system... – tjwrona1992 Sep 16 '15 at 16:40