Last active
December 17, 2015 05:18
-
-
Save nathania/5556580 to your computer and use it in GitHub Desktop.
Named formatting in python print statements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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