Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Python % formating (See related posts)

# format % 2
%d     : '2'
%5d    : '    2'
%-5d   : '2    '
%05d   : '00002'
%.2e   : '2.00e+000'
%.2f   : '2.00'

%s     : string, applying str()
%-20s  : left-adjust

Comments on this post

PhoenixR posts on Jun 12, 2006 at 10:58
%(var_name)s : string, match the var_name, so that you can use a dict. for example:

values = {'first_name' : 'Phoenix', 'last_name' : 'R' }
print "My name is %(first_name)s %(last_name)s." % values


the result will be:
My name is Phoenix R.
PhoenixR posts on Jun 12, 2006 at 10:59
by the way, this seems to have a formal name: string interpolating or something similar. ^_^

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts