%s %x %0.2f %04d

Alex Martelli aleaxit at yahoo.com
Sat Sep 9 01:00:59 CEST 2000


"Brendhan Horne" <brendhan at bellsouth.net> wrote in message
news:HEcu5.16138$RY2.177969 at news4.mco...
> 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
>
> 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.

Good point!  The tutorial is not optimal on this (and nor is the
reference...!).

When you're using the % operator with the left operand being
a number, i.e., the "modulus" as you call it, there are no
'suffixes':

>>> print 7 % 2
1

The notation in the subject refers to the case in which the
left operand of % is a STRING, in which case % is the
'format' operator, not 'modulus'.  The %s etc are things
you put in the STRING that is the left operand.

>>> print "Before %s After" % 7
Before 7 After

>>> print "Before %x After" % 15
Before f After

>>> print "Foo %04d Bar" % 3*3
Foo 0009 Bar


Alex






More information about the Python-list mailing list