String formatting characters - looking for details

Remco Gerlich scarblac at pino.selwerd.nl
Thu Feb 8 06:36:07 EST 2001


Martin Bless <m.bless at gmx.de> wrote in comp.lang.python:
> (1) Where can I find some more details about the string formatting
> characters?
> (2) How  I print hex values with zeros padded left, like 0F or 0A?
> print '%X' % 15  # two digits, wanted

'%02X'. '%2X' pads with spaces so it is width 2, '%02X' pads with zeroes.

> (3) This is from the Python docs - I'm looking for more information on
> how to use the formatting characters.

Well, the Python docs say what you can use, not what it does... The best
option is probably to look at a Unix man page for printf. Type 'man printf'
at a Unix/Linux prompt or into Google.

> ------------------- from the Python docs --------
> 2.1.5.2 String Formatting Operations 
> 
> String objects have one unique built-in operation: the % operator
> (modulo) with a string left argument interprets this string as a C
> sprintf() format string to be applied to the right argument, and
> returns the string resulting from this formatting operation. 
> 
> The right argument should be a tuple with one item for each argument
> required by the format string; if the string requires a single
> argument, the right argument may also be a single non-tuple object.2.5
> The following format characters are understood: %, c, s, i, d, u, o,
> x, X, e, E, f, g, G. Width and precision may be a * to specify that an
> integer argument specifies the actual width or precision. The flag
> characters -, +, blank, # and 0 are understood. The size specifiers h,
> l or L may be present but are ignored.
> ---------------------
 
-- 
Remco Gerlich



More information about the Python-list mailing list