How to evaluate environment variables into a string in Python?

Use os.path.expandvars to expand the environment variables in the string, for example:

>>> os.path.expandvars('$C/test/testing')
'/stackoverflow/test/testing'

In Python 3 you can do:

'{VAR}'.format(**os.environ))

for example

>>> 'hello from {PWD}'.format(**os.environ))
hello from /Users/william