string formatting using the % operator

Peter Hansen peter at engcorp.com
Mon Jun 13 12:48:50 EDT 2005


Dan Sommers wrote:
> Let the DB-API do more work for you:
> 
>     cursor = connection.cursor( )
>     sql = """SELECT column2, columns3 FROM table WHERE name LIKE %s"""
>     values = ('%%%s%%' % searchterm,) # note that this is a tuple

It looks like this might be a rare case where not using the % operator 
might make it easier to see what's going on:

       values = ('%' + searchterm + '%',)

-Peter



More information about the Python-list mailing list