string formatting using the % operator
fargo
fargo_01 at hotmail.com
Mon Jun 13 11:24:03 EDT 2005
William Gill wrote:
> I am using the % operator to create queries for a db app. It works fine
> when exact strings, or numbers are used, but some queries need partial
> matching that use the '%' as a wildcards. So for example the resultant
> string should be 'WHERE name LIKE %smith%' (would match silversmith,
> smithy, and smith). Is there any way to get something like
>
> searchterm = 'smith'
> sql += 'WHERE name LIKE %s' % searchterm
>
> to return 'WHERE name LIKE %smith%' I have tried using escapes,
> character codes for the % sign, and lots of other gyrations with no
> success. The only thing that works is if I modify searchterm first:
>
> searchterm = 'smith'
> searchterm ='%'+'smith'+'%'
> sql += 'WHERE name LIKE %s' % searchterm
>
> Any Ideas?
try this :
sql += 'WHERE name LIKE %%%s%%' % searchterm
More information about the Python-list
mailing list