Python Format Codes

The string.format function requires any variables to be included in the string to be listed in the following format:

{[identifier][:format spec]}

Multiple variables can be inserted inside the format argument. But as the function expects multiple arguments, iterators have to be dereferenced before being used:

print(‘The vector in pointing to ({}, {}, {}) in cartesian notation’.format(2, 7, 4)

the vector is pointing to (2, 7, 4) in cartesian notation

3d_vector = [2, 7, 4]

print(‘The vector is pointing to ({}, {}, {}) in cartesian notation’.format(*3d_vector))

the vector is pointing to (2, 7, 4) in cartesian notation

Identifiers

Identifiers can either be numeric—positions in the argument list—or keywords for a dictionary. For example:

print(‘Hello {0} {1}’.format(‘Cleese’, ‘John’))

Hello John Cleese

print(‘Hello {first} {last}’.format({‘last’:’Idle’, ‘first’:’Eric’}))

Hello Eric Idle

When no identifiers are specified all formatting codes are id’d by their position.

print(‘The two most common professions are {} and {}’.format(‘lumberjack’, ‘bicycle repairman’})

The two most common professions are lumberjack and bicycle repairman

Formatting Specification

The format specification consists of the following elements:

:[fill character][alignment][sign][#][0][width][,][.precision][type]

Note that all options are chosen in such a way that each of them can be unambigiously left out.

Fill character Default is space; specifies the fill character for (if any) unused positions
Alignment One of the following alignment characters:
< Left align; default for most objects
> Right align; default for numbers
= Spread out; place fill characters between the sign and the number
^ Center within the available space
Sign Only for numbers, can only be one of these:
+ Use sign for both positive and negative numbers
Only use sign for negative numbers (this is the default)
‘ ‘ A space indicates that a space should be used for positive numbers, and a minus sign for negative ones
# Only for binary, octal and hexadecimal notation, to show the 0b, 0O and 0x prefix respectively
0 Used if the width is specified to indicate that the number should be padded with leading zeroes
width A number indicating the minimum field width
, The comma is used to indicate the use of thousands-separators
precision Precision defines how many digits are shown after the decimal point (or total number of digits in floating point notation)
type For string type variables, the following options are available:
s Default; string type representation
For integer type variables, the following options are available:
b Binary format; outputs numbers in base 2
c Character; converts the number to the associated unicode character before printing
d Default; decimal format, outputs numbers in base 10
o Octal format; outputs numbers in base 8
x Hexadecimal format; outputs numbers in base 16 with lower case letters
X Hexadecimal format; outputs numbers in base 16 with upper case letters
n Number; this is the same as decimal format (d) except that it uses separator symbols according to the system locale
For float and decimal type variables, the following options are available:
e Exponent (scientific notation) format, using the [mantisse]e[exponent] notation
E Exponent (scientific notation) format, same as ‘e’ except using an upper case ‘E’
f Fixed point. The default precision is 6
F Fixed point. Same as ‘f’
g Default; general format. Basically fixed point unless the number doesn’t fit in the field, in which case formatting is switched over to scientific notation
G Same as ‘g’ but any letters are printed in upper case
n Number format; same as ‘g’ but using system locale for decimal point and thousand separators
% Percentage. Number is multiplied with 100 and shown in precision indicated, followed by a percent sign
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s