0

I just started using Python's ConfigFile.SafeConfigParser class to parse a config file that contains variable definitions and references. It works nicely, but I can't find an explanation for the weird syntax for a variable reference:

BASEPATH = C:\Users\me\x
SOME_FILE_PATH = %(BASEPATH)s\a
# Yields C:\Users\me\x\a

What's the 's' for? Are there other characters that make the variable behave differently?

Jonathan Sachs
  • 613
  • 8
  • 19

1 Answers1

0

The syntax comes from Python's string formatting syntax, which is based on C printf syntax.

In short, %s means "substitute with a string", %(abc)s means "substitute with a string named abc":

For example:

>>> print "Hello %s!" % 'world'
Hello world!
>>> print "Hello %(name)s!" % dict(name='world')
Hello world!
zvone
  • 18,045
  • 3
  • 49
  • 77