Python formatting codes - University of Cambridge
Python formatting codes
The on-line python documentation for the complete set of formatting codes can be found at
and goes further than this quick document. We have tried to select the most useful codes here.
Strings
We consider the string "Hello, world!".
Formatting code %s %20s %-20s %3s
Hello, world! 'Hello, world' ' Hello, world!' 'Hello, world! ' 'Hello, world'
Integers
We consider the integers 12,345 and -12,345.
Formatting code %d %20d %-20d %020d %+d %+20d %+-20d %+020d %3d
12,345
'12345'
'
12345'
'12345
'
'00000000000000012345'
'+12345'
'
+12345'
'+12345
'
'+0000000000000012345'
'12345'
-12,345
'-12345'
'
-12345'
'-12345
'
'-0000000000000012345'
'-12345'
'
-12345'
'-12345
'
'-0000000000000012345'
'-12345'
Floating point numbers We consider the floating point numbers 12?34567 and -12?34.
The %f formatting code presents data in decimal notation. The %e code does it in exponential form.
Formatting code %f %20f %-20f %020f %+f %+20f %+-20f %+020f %.4f %20.4f %-20.4f %020.4f %+.4f %+20.4f %+-20.4f %+020.4f
12?34567
'12.345670'
'
12.345670'
'12.345670
'
'0000000000012.345670'
'+12.345670'
'
+12.345670'
'+12.345670
'
'+000000000012.345670'
'12.3457'
'
12.3457'
'12.3457
'
'000000000000012.3457'
'+12.3457'
'
+12.3457'
'+12.3457
'
'+00000000000012.3457'
-12?34
'-12.340000'
'
-12.340000'
'-12.340000
'
'-000000000012.340000'
'-12.340000'
'
-12.340000'
'-12.340000
'
'-000000000012.340000'
'-12.3400'
'
-12.3400'
'-12.3400
'
'-00000000000012.3400'
'-12.3400'
'
-12.3400'
'-12.3400
'
'-00000000000012.3400'
Formatting code %e %20e %-20e %020e %+e %+20e %+-20e %+020e %.4e %20.4e %-20.4e %020.4e %+.4e %+20.4e %+-20.4e %+020.4e
12?34567
'1.234567e+01'
' 1.234567e+01'
'1.234567e+01 '
'000000001.234567e+01'
'+1.234567e+01'
' +1.234567e+01'
'+1.234567e+01 '
'+00000001.234567e+01'
'1.2346e+01'
'
1.2346e+01'
'1.2346e+01
'
'00000000001.2346e+01'
'+1.2346e+01'
'
+1.2346e+01'
'+1.2346e+01
'
'+0000000001.2346e+01'
-12?34
'-1.234000e+01'
' -1.234000e+01'
'-1.234000e+01 '
'-00000001.234000e+01'
'-1.234000e+01'
' -1.234000e+01'
'-1.234000e+01 '
'-00000001.234000e+01'
'-1.2340e+01'
'
-1.2340e+01'
'-1.2340e+01
'
'-0000000001.2340e+01'
'-1.2340e+01'
'
-1.2340e+01'
'-1.2340e+01
'
'-0000000001.2340e+01'
Formatting graphs
Some people find flow charts easy to read. If you are one of these people you may find the following two graphs an aid to understanding formatting. If you're not; don't worry about it.
In both charts, N is the number of characters assigned to the formatted representation of the number. In the floating point chart, M is the number of decimal places.
-
+
left
justified ?
0
% Always show sign?
fixed width?
zero padded
?
N
d
+
% Always show sign?
-
left
justified ?
0
fixed width?
zero padded
?
N
.M f
specify precision
?
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- scientific and mathematical computing using python
- pandas format numbers with commas
- s e c t i o n 2 4 p o w e r f u l o u t p u t f o r m
- numbers lists and tuples
- str s format method for introduction to programming using
- python course rutgers university
- introduction to python university of pennsylvania
- programming in python 3 smu
- using python in labeling and field calculations esri
- formatting numbers with c output streams
Related searches
- university of minnesota college of education
- university of minnesota school of social work
- wharton school of the university of pennsylvania
- bar codes country of origin
- university of cambridge acceptance rate
- university of cambridge biology
- formatting for statement of purpose
- python formatting currency
- python formatting types
- area codes state of ohio
- university of maryland university college md
- python formatting output