semi-concatenated strings

Skip Montanaro skip at pobox.com
Thu May 30 16:39:20 EDT 2002


    Grant> I discovered today that strings can sometimes be concatenated
    Grant> without using a "+":

Actually, string literals can always be concatenated without adding them.

    Grant> I discovered this, of course, while making a mistake like this:

    >>> a = ['zero', 'one'
    ...       'two', 'three']
    >>> a
    ['zero', 'onetwo', 'three']

Yup, that's just how it's supposed to work.  It makes it easier to compose
long strings.  For example, I have code like this that queries a database:

    rows = self.executesql("select cities.city, state, country"
                           "    from cities, venues, events, addresses"
                           "    where     cities.city like %s"
                           "          and events.active = 1"
                           "          and venues.address = addresses.id"
                           "          and addresses.city = cities.id"
                           "          and events.venue = venues.id",
                           (city,))

At compile time all those strings are concatenated into one long string.
The select statement remains readable for me, but is represented as a single
string constant in the generated bytecode.

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)
Boycott Netflix - they spam - http://www.musi-cal.com/~skip/netflix.html






More information about the Python-list mailing list