String Format Operator Directives


When using the string format operator (see Table B.5), you may enhance or fine-tune the object display with the directives shown in

Table B.5. String Format Operator Conversion Symbols

Format Symbol

Conversion

%c

Character (integer [ASCII value] or string of length 1)

%r[a]

String conversion via repr() prior to formatting

%s

String conversion via str() prior to formatting

%d / %i

Signed decimal integer

%u[b]

Unsigned decimal integer

%o[b]

(Unsigned) octal integer

%x[b] / %X[b]

(Unsigned) hexadecimal integer (lower/UPPERcase letters)

%e / %E

Exponential notation (with lowercase 'e'/UPPERcase 'E')

%f / %F

Floating point real number (fraction truncates naturally)

%g / %G

The shorter of %e and %f/%E% and %F%

%%

Percent character ( % ) unescaped


[a] New in Python 2.0; likely unique only to Python.

[b] %u/%o/%x/%X of negative int will return a signed string in Python 2.4+.

Table B.6. Format Operator Auxiliary Directives

Symbol

Functionality

*

Argument specifies width or precision

-

Use left justification

+

Use a plus sign ( + ) for positive numbers

<sp>

Use space-padding for positive numbers

#

Add the octal leading zero ('0') or hexadecimal leading '0x'

or '0X', depending on whether 'x' or 'X' were used

0

Use zero-padding (instead of spaces) when formatting numbers

%

'%%' leaves you with a single literal '%'

(var)

Mapping variable (dictionary arguments)

m.n

m is the minimum total width and n is the number of digits to display after the decimal point (if applicable)




Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net