Python has no "printf" or "sprintf" - use the % operator on a string object which is the format string instead. Let me show you that ...
Firstly, what do we get if we do NOT format?
>>> val = 1. / 17.
>>> print val
0.0588235294118
Here it is, formatted to 4 figures after the decimal place:
>>> val24dp = "%.4f" % val
>>> print val24dp
0.0588
And with a total field width of 8 characters, of which 4 are after the decimal place (the 8 is a total width and NOT the number of places before!)
>>> f2 = "A seventeenth is %8.4f to 4 places" % val
>>> print f2
A seventeenth is 0.0588 to 4 places
Whole numbers are formatted with a "d" - that stands for "decimal" as opposed to "x" for hexadecimal, or the letter "o" for Octal.
>>> n = 5
>>> vv = "There are %3d in stock" % n
>>> print vv
There are 5 in stock
And if I want to format several values at the same time, I can do so by using multiple % based place holders, and then specifying a tuple for formatting.
>>> vx = "There are %d in stock at %.4fp each" % (n,val)
>>> print vx
There are 5 in stock at 0.0588p each
(written 2010-05-25)
Associated topics are indexed under
Y108 - Python - String Handling [4027] Collections in Python - list tuple dict and string. - (2013-03-04)
[3886] Formatting output - why we need to, and first Python example - (2012-10-09)
[3796] Backquote, backtic, str and repr in Python - conversion object to string - (2012-07-05)
[3469] Teaching dilemma - old tricks and techniques, or recent enhancements? - (2011-10-08)
[3468] Python string formatting - the move from % to str.format - (2011-10-08)
[3349] Formatting output in Python through str.format - (2011-07-07)
[3218] Matching a license plate or product code - Regular Expressions - (2011-03-28)
[3090] Matching to a string - what if it matches in many possible ways? - (2010-12-17)
[2814] Python - splitting and joining strings - (2010-06-16)
[2765] Running operating system commands from your Python program - (2010-05-14)
[2721] Regular Expressions in Python - (2010-04-14)
[2692] Flexible search and replace in Python - (2010-03-25)
[2406] Pound Sign in Python Program - (2009-09-15)
[2284] Strings as collections in Python - (2009-07-12)
[1876] Python Regular Expressions - (2008-11-08)
[1608] Underlining in Perl and Python - the x and * operator in use - (2008-04-12)
[1517] Python - formatting objects - (2008-01-24)
[1195] Regular Express Primer - (2007-05-20)
[1110] Python - two different splits - (2007-03-15)
[970] String duplication - x in Perl, * in Python and Ruby - (2006-12-07)
[954] Splitting Pythons in Bradford - (2006-11-29)
[943] Matching within multiline strings, and ignoring case in regular expressions - (2006-11-25)
[903] Pieces of Python - (2006-10-23)
[773] Breaking bread - (2006-06-22)
[560] The fencepost problem - (2006-01-10)
[496] Python printf - (2005-11-15)
[463] Splitting the difference - (2005-10-13)
[324] The backtick operator in Python and Perl - (2005-05-25)
5708
Some other Articles
Course LunchesThe Perl SurveyTell the Melksham Chamber or you only have yourself to blameThe 500 pound question to get you startedFormatted Printing in PythonThe history and future of Melksham Chamber of CommerceLearning to program in Python 2 ... and / or in Python 3Blowing our own trumpetRegional Spatial Strategy and the next 20 years - Whence Wiltshire under the new government?Declarations of interest - knowing where you stand with people