String concatenation

Leif B. Kristensen junkmail at solumslekt.org
Fri Apr 2 02:47:47 EST 2004


Having recently started with Python, I've written this little function
to retrieve place parts from a database and concatenate them to a
string. While it certainly works, and is also considerably shorter than
the PHP code that I originally wrote, I'm pretty convinced that there
should be an even better way to do it. Can anybody show me how to write
the string concatenation part in a more Pythonesque syntax?

def get_place(x):
    c=db.cursor()
    c.execute("select place_lvl1, place_lvl2, \
                place_lvl3, place_lvl4, place_lvl5 \
                from place where place_id = %d" % (x))
    res=c.fetchone()
    place = ''
    for i in range(5):
        tmp = res[i]        
        if tmp[:1] != '-' and len(tmp) != 0:
            place = place + ', ' + (res[i])
    return place[2:]

regards,
-- 
Leif Biberg Kristensen
http://solumslekt.org/
Validare necesse est



More information about the Python-list mailing list