%s %x %0.2f %04d
sholden at holdenweb.com
Fri Sep 8 23:37:44 CEST 2000
Brendhan Horne wrote:
> In the beginners instruction manual it states:
> There are other letters that can be placed after the % markers. Some of
> those include:
> %s- for string
> %x- for hexadecimal number
> %0.2f- for a real number with a maximum of 2 decimal places
> %04d- pad the number out to 4 digits with 0's
These "% markers" go in a string on the left-hand side of the
% operator. On the right-hand side you need a tuple (that is,
for your purposes, a comma-separated list of values inside
parentheses) of values. Each value in the tuple will correspond
to one of the "% markers".
In the simplest case where you are only formatting one value you are
allowed to omit the parentheses around the tuple.
The result is a string with the % markers replaced with the values,
appropriately formatted. See below for some examples.
> That is nice but it gives no examples of how to use them. I tried some of
> the basics
> >>> print 7%s2
> Got an error
> >>> print 7 %s 2
> Got an error
> So if someone could show me some basic one liners that use the modulus with
> the suffix I would appreciate it. Remember when you do this you are dealing
> with someone who has no knowledge of computer language.
>>> "My string is: %s" % "XXX"
'My string is: XXX'
>>> "%d bottles of %s on the wall" % (99, "beer")
'99 bottles of beer on the wall'
>>> "%d in hexadecimal is %x" % (99, 99)
'99 in hexadecimal is 63'
>>> "%d bottles of %s on the wall" % (1017, 'champagne')
'1017 bottles of champagne on the wall'
Hope this gets you started.
Helping people meet their information needs with training and technology.
703 967 0887 sholden at bellatlantic.net http://www.holdenweb.com/
More information about the Python-list