index

Python-2.5 Easy Tutorial for Beginners:   String Formatting


Formula: 'format' % (values)


Each format specifier should correspond to an item in the values by position, and values must have exactly as many items as format has specifiers (plus one extra for each width or precision given by *).


Itemsin the Values can be put in:



Format Specifier= %[(name)][flag][width][.precision]code


format_specifier

(name) flag width precision code


Click on the nice buttons



Name (works only with dictionaries):


>>> S = '%(name1)s' % {'name1':'hello', 'name2':'world'}

>>> print S

hello


>>> S = '%(name2)s %(name3)d' % {'name1':'hello', 'name2':'World', 'name3':2999}

>>> print S

World 2999



The trick is often used in  conjunction with vars built-in function, which returns a dictionary containing all the variables that exist in the place:


>>> greeting = 'hello'

>>> vars ( )

{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'S': 'hello', '__doc__': None, 'greeting': 'hello'}

>>> S = '%(greeting)s' % vars ( )

>>> S

'hello'


When name is given, cannot be used.

There's no way to put an integer into a dictionary.



Flags:


#

Alternate Form (defined below)

0

The conversion will be zero padded for numeric values.

-

The converted value is left adjusted (overrides the "0" conversion if both are given)


(a space) A blank should be left before a positive number (or empty string) produced by a signed conversion.

+

A sign character ("+" or "-") will precede the conversion (overrides a "space" flag).

 


Flags Usage:


Flag

Meaning

Code Examples

Description

#



Forced dot

with Floats


e

E





f

F






g

G

>>> S = '%.0e' % 1

>>> print S

1e+00

>>> S = '%#.0e' % 1

>>> print S

1.e+00



>>> S = '%.0F' % 1

>>> print S

1

>>> S = '%#.0F' % 1

>>> print S

1.



>>> S = '%g' % 1

>>> print S

1

>>> S = '%#g' % 1

>>> print S

1.00000



The alternate form "#" causes the result to always contain a decimal point, even if no digits follow it. The precision  determines the number of digits after the decimal point and  defaults to 6.



The alternate form "#"  causes the result to always contain a  decimal point, and trailing zeroes are not removed as they would otherwise be. The precision determines the number of significant digits before and after the decimal point and defaults to 6.

Forced leading zero ("0")

with Octals


o

>>> S = '%.o' % 1

>>> print S

1

>>> S = '%#.o' % 1

>>> print S

01



The alternate form "#" causes a leading zero ("0") to be inserted  between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.

Forced leading zero "0x"

with Hexadecimal


x

X

>>> S = '%x' % 1

>>> print S

1

>>> S = '%#x' % 1

>>> print S

0x1



The alternate form "#" causes a leading '0x' or '0X' (depending on whether the "x" or "X" format was used) to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.

0

 Only for numeric values


d






e








f








x

>>> S = '%6d' % 1

>>> print S

     1

>>> S = '%06d' % 1

>>> print S

000001



>>> S = '%9.0e' % 1

>>> print S

    1e+00

>>> S = '%09.0e' % 1

>>> print S

00001e+00



>>> S = '%6.2f' % 1.0

>>> print S

  1.00

>>> S = '%06.2f' % 1.0

>>> print S

001.00



>>> S = '%6x' % 1

>>> print S

     1

>>> S = '%06x' % 1

>>> print S

000001




The conversion will be zero padded for numeric values.



-

Value is left adjusted


s



i





f





o

>>> S = '%-6s' % '1'

>>> S

'1     '



>>> S = '%-6i' % 1

>>> S

'1     '



>>> S = '%-6.0f' % 1

>>> S

'1     '



>>> S = '%-6.0o' % 1

>>> S

'1     '



The converted value is left adjusted (overrides the "0" conversion if both are given).



-

Inserts spaces


s



d

>>> S = '% 6s' % ''

>>> S

'      '



>>> S = '% 6d'% -1

>>> S

'    -1'



(a space) A blank should be left before a positive number (or  empty string) produced by a signed conversion.


+

Inserts +

(with integers)

>>> S = '%+6s' % 1

>>> S

'     1'



>>> S = '%+6s' % 1

>>> S

'     1'

>>> S = '%+6i' % 1

>>> S

'    +1'

>>> S = '%+6f' % 1

>>> S

'+1.000000'

>>> S = '%+6e' % 1

>>> S

'+1.000000e+00'

>>> S = '%+6d' % 1

>>> S

'    +1'

>>> S ='%-+*.*f' % (10, 2, 5)

>>> S

'+5.00     '

A sign character ("+" or "-") will precede the conversion (overrides a "space" flag).





Width: 

  • digit or
  • that means that the width is taken from the next item in values


>>> S = '%10s' % 'Hello!'

>>> S

'    Hello!'


>>> S = '%*s' % (10,'Hello!')

>>> S

'    Hello!



Precision:

  • digit or
  • that means that the width is taken from the next item in values


>>> S = '%.2s' % 'Hello!'

>>> S

'He'

>>> S = '%.*s' % ( 2, 'Hello!')

>>> S

'He'





Code:


Conversion

Meaning

d

Signed integer decimal.

i

Signed integer decimal.

o

Unsigned octal.

u

Unsigned decimal.

x

Unsigned hexadecimal (lowercase).

X

Unsigned hexadecimal (uppercase).

e

Floating point exponential format (lowercase).

E

Floating point exponential format (uppercase).

f

Floating point decimal format.

F

Floating point decimal format.

g

Floating point format. Uses exponential format if exponent is greater than -4 or less than precision, decimal format otherwise.

G

Floating point format. Uses exponential format if exponent is greater than -4 or less than precision, decimal format otherwise.

c

Single character (accepts integer or single character string).

r

String (converts any python object using repr()).

s

String (converts any python object using str()).

%

No argument is converted, results in a "%" character in the result.



© Copyright 2007    Alex Ivanov    www.3dplumbing.net

contact me

top     index

Valid HTML 4.01 Transitional