'concatenating''strings'

Bengt Richter bokr at oz.net
Thu Aug 8 14:34:48 EDT 2002


On 8 Aug 2002 11:04:54 -0700, hwlgw at hotmail.com (Will Stuyvesant) wrote:

>Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on
>win32
>Type "copyright", "credits" or "license" for more information.
>IDLE 0.8 -- press F1 for help
>>>> '1''2'
>'12'
>>>> '1''2''3'
>'123'
>>>> '1'''
>'1'
>>>> '''3'
>KeyboardInterrupt
>>>> # that did hang with the cursor on the next line so I had to press
>CTRL-c
>>>> _
>'1'
>>>> ''+'3'
>'3'
>
>Totally unimportant but unexpected behaviour.
>Looks like you can not concatenate the empty string '' to 'something'
>without using the + operator.
No, you stumbled onto triple quotes, which can enclose other quotes and
new lines, and expects to be closed with the same kind of triple quotes.
It was just waiting for you to continue inputting the string ;-)

BTW, the automatic concatenation will ignore intervening spaces, so:
 >>> '' '3'
 '3'

Or immediately closing the triple quotes
 >>> '''3'''
 '3'
which is not the same as
 >>> '' '3' ''
 '3'

Further illustrating:

 >>> s = '''3
 ... "hello" 'hello' """hello""", but not
 ... ''\'hello'\'' without an escape to
 ... prevent seeing 3 's as a terminating quote.'''
 >>> print s
 3
 "hello" 'hello' """hello""", but not
 '''hello''' without an escape to
 prevent seeing 3 's as a terminating quote.

You can do the same as above with roles of
single and double quotes swapped.

Regards,
Bengt Richter



More information about the Python-list mailing list