problem with % operator on format string

John Hunter jdhunter at ace.bsd.uchicago.edu
Wed Jun 25 22:27:52 EDT 2003


>>>>> "vald" == vald  <vald at valis.amber.eu.org> writes:

    vald>  query = "select date(xxx, '\%d') from yyy where xxx like
    vald> '%s%s\%'" % (year, month)

    vald> so it should produce something like that (year=2003,
    vald> month=06)

    vald>  select date(xxx, '%d') from yy where xxx like '200306%'

To make a literal %, use %%.  

You are looking for

  query = "select date(xxx, '%%d') from yy where xxx like '%d%02d%%'" % (2003, 6)

The %02d says to pad the second integer with zeros up to length 2.

Check out http://www.python.org/doc/current/lib/typesseq-strings.html
for the details.

John Hunter





More information about the Python-list mailing list