Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python printf
To format data into a string in python, use the % operator as python doesn't have an sprintf or printf function. So why have I entitled today's entry "python printf? Because this in one of the most frequently asked, and infrequently answered questions.

The % operator with a string to its left formats the value to the right using the (sprintf style) string to to the left of the operator. Where you want to format more that one value, use a tuple to the right.

Here's an example in which I've formatted an integer to return a three digit number padded with leading zeros:

>>> licensed = 7
>>> callin = "Agent %03d" % licensed
>>> print callin
Agent 007
>>>


We've got an example using a tuple in our course material and there's an example using a format string variable and aligning output columns in python there too. There is further documentation of pythons' % operator on the Python documentation site (well hidden, so I give you the link!)

Aside - why no printf in python? Because you don't need it and the alternative is neater and better. Arguably, there shouldn't be a printf function in any other language either, as it both formats the values AND sends them to the output channel - doing two logical operations in a single call, and you're bound to want to separate them at some stage. Instead, there should be two functions. There usually are ... and from a purist's viewpoint, sprintf followed by echo or print is much more maintainable that a single sprintf.
(written 2005-11-15 23:13:47)

 
Associated topics are indexed under
Y108 - Python - String Handling

Back to
More spam - a success story
Previous and next
or
Horse's mouth home
Forward to
I have a river to cross

Some other Articles
Bowerhill, Melksham, 2006 Calendar
Good IT training cannot be cheap
Why is Tomcat called Tomcat?
I have a river to cross
Python printf
More spam - a success story
MySQL - a score of things to remember
Running a Perl script within a PHP page
New Navigation Aid - Launch of My Wellho
Being atypical is typical
1633 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho