[Tutor] Surprised that print("a" "b") gives "ab"

boB Stepp robertvstepp at gmail.com
Sat Mar 5 23:25:51 EST 2016


I stumbled into finding out that

>>> "a" "b"
'ab'

or

>>> print("a""b")
ab

Note that there is not even a space required between the two strings.

I find this is surprising to me as "a" "b" is much less readable to me
than "a" + "b" .  And since Python is all about easily readable code,
why was this feature implemented?  Is there a use case where it is
more desirable to not have a string concatenation operator explicitly
used?  The only thing that comes to my mind are strings spread over
multiple lines, say

print("This will be a ..."
    "...very long string...")

But is this preferable to

print("This will be a ..." +
    "...very long string...")

?  I personally prefer the latter, so I am probably missing something.

BTW, I am following through (So far!) on my New Year's resolution to
persistently work through some Python books.  I've knocked off "Python
Crash Course", which I think is a fine beginner's book.  Now I am
starting "Think Python, 2nd ed.", which is an intro to C.Sc.-type
book.  So I imagine I may come up more questions along the lines of
today's--be forewarned!

-- 
boB


More information about the Tutor mailing list