semi-concatenated strings

Chris Liechti cliechti at gmx.net
Thu May 30 17:26:13 EDT 2002


Steven Majewski <sdm7g at Virginia.EDU> wrote in
news:Pine.OSX.4.43.0205301633130.5638-100000 at d-128-61-180.bootp.Virginia.
EDU: 
> On 30 May 2002, Grant Griffin wrote:
>> I discovered today that strings can sometimes be concatenated without
>> using a "+":
> 
>>    >>> a = 'one' ' plus ' 'two'
>>    >>> a
>>    'one plus two'
> 
> [...]
> 
>> Is there some grand purpose here, or is this just a bug in the
>> parser? 
> 
> It's a feature, not a bug:
> 
> I believe that it predates the triple quoted strings as a way to have
> very long multiline strings. ( I'ld have to dig thru the news archives
> or the changelog to be certain about the history. )
> 
> (There should only be HOW MANY WAYS?! ;-)

it has applications where only this method works.
i think the example is in the docs. e.g. when you want to comment on a 
complicated string, like an regex:

re.compile("([ab])+"   #group one (at least one occurence of either a or b)
           "(hello)*"  #group two (optional)
          )

(ok + works too but its not the same. the "'a' 'b'" concatenated string is 
one, with + you have many strings that are concatenated at runtime.)

the feature is also known in C, where it's important to do some 
preprocessor magic.

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list