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

boB Stepp robertvstepp at gmail.com
Sun Mar 6 02:03:01 EST 2016


On Sun, Mar 6, 2016 at 12:37 AM, Ben Finney <ben+python at benfinney.id.au> wrote:
> boB Stepp <robertvstepp at gmail.com> writes:
>
>> […] why was this feature implemented?
>
> You've been asking that a lot lately :-) This forum is unlikely to be
> able to give authoritative answers to “why was the language designed the
> way it is?”.

While I *am* quite interested in the why's of Python's design, my
question was actually intended to find out in what situation(s)
implicit string concatenation is a better choice than the explicit
approach.  And you gave me something to think about later in your
response.

I have to add, though, that I *do* think it is useful in this forum to
consider the design choices that led to Python's feature set and
syntax.  Not at an incredibly deep level, but, instead, to acquire the
pythonista's philosophy of programming.  I think that understanding
such things will help me to use the language more in line with how it
is meant to be used.

>> 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.
>
> It is salient to this issue, that the two cases above are semantically
> different.
>
> The first one is defined (by the syntax for literals) to create a
> *single* string object. Semantically, the fragments are specifying one
> object in a single step.
>
> The second is semantically (i.e. by the semantics of how such
> expressions are defined to work) creating two distinct objects, then
> creating a third using an operation, then discarding the first two.

Under the hood, are the two approaches above done in the same way?  I
get your semantics point, but are there two string objects created in
both approaches or does the first in fact create only a single object?
 If the first truly only creates a single object, then it seems that
this is a more efficient approach.

-- 
boB


More information about the Tutor mailing list