building strings from variables

MonkeeSage MonkeeSage at gmail.com
Fri Oct 6 04:04:00 CEST 2006


Ps. For readability you can also roll your own sprintf function:

def format(s, *args, **kwargs):
  if args:
    return s % args
  elif kwargs:
    return s % kwargs
  else:
    return s

s = 'I like %s and %s.'
print format(s, 'ham', 'cheese')

s = 'I like %(b)s and %(c)s.'
print format(s, b='butter', c='eggs')

> Regards,
> Jordan




More information about the Python-list mailing list