Skip to content

Instantly share code, notes, and snippets.

@nathania
Last active December 17, 2015 05:18
Show Gist options
  • Save nathania/5556580 to your computer and use it in GitHub Desktop.
Save nathania/5556580 to your computer and use it in GitHub Desktop.
Named formatting in python print statements.
data = dict(state='active', canonical_name='un#ep')
print('Endpoint %(canonical_name)s is %(state)s' % data)
print('Endpoint {canonical_name} is {state}'.format(**data))
foo, bar = 'foo', 'bar'
print('%(foo)s%(bar)s' % locals())
print('{foo}{bar}'.format(**locals()))
print('{foo}{bar}'.format(foo='foo', bar='bar')) # how much faster is % formatting?
# TODO: is nesting possible?
# http://stackoverflow.com/questions/101268/hidden-features-of-python#113164
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment