Home > Articles > Programming > Python

📄 Contents

  1. Operators
  2. Formatting Strings—Modulus
  3. Summary
  • Print
  • + Share This
This chapter is from the book

Formatting Strings—Modulus

Although not actually modulus, the Python % operator works similarly in string formatting to interpolate variables into a formatting string. If you've programmed in C, you'll notice that % is much like C's printf(), sprintf(), and fprintf() functions.

There are two forms of %, one of which works with strings and tuples, the other with dictionaries.

StringOperand % TupleOperand 
StringOperand % DictionaryOperand 

Both return a new formatted string quickly and easily.

% Tuple String Formatting

In the StringOperand % TupleOperand form, StringOperand represents special directives within the string that help format the tuple. One such directive is %s, which sets up the format string

>>> format = "%s is my friend and %s is %s years old" 

and creates two tuples, Ross_Info and Rachael_Info.

>>> Ross_Info = ("Ross", "he", 28)
>>> Rachael_Info = ("Rachael", "she", 28)

The format string operator (%) can be used within a print statement, where you can see that every occurrence of %s is respectively replaced by the items in the tuple.

>>> print (format % Ross_Info) 
Ross is my friend and he is 28 years old 

>>> print (format % Rachael_Info) 
Rachael is my friend and she is 28 years old 

Also note that %s automatically converts the last item in the tuple to a reasonable string representation. Here's an example of how it does this using a list:

>>> bowling_scores = [190, 135, 110, 95, 195]
>>> name = "Ross"
>>> strScores = "%s's bowling scores were %s" \
...                                                 % (name, bowling_scores) 
>>> print strScores 
Ross's bowling scores were [190, 135, 110, 95, 195] 

First, we create a list variable called bowling_scores and then a string variable called name. We then use a string literal for a format string (StringOperand) and use a tuple containing name and bowling_scores.

Format Directives

Table 3–6 covers all of the format directives and provides a short example of usage for each. Note that the tuple argument containing a single item can be denoted with the % operator as item, or (item).

Table 3–6 Format Directives

Directive

Description

Interactive Session

%s

Represents a value as a string

>>> list = ["hi", 1, 1.0, 1L]

>>> "%s" % list

"['hi', 1, 1.0, 1L]"

>>> "list equals %s" % list

"list equals ['hi', 1, 1.0, 1L]"

%i

Integer

>>> "i = %i" % (5)

'i = 5'

>>> "i = %3i" % (5)

'i = 5'

%d

Decimal integer

>>> "d = %d" % 5

'd = 5'

>>> "%3d" % (3)

' 3'

%x

Hexadecimal integer

>>> "%x" % (0xff)

'ff'

>>> "%x" % (255)

'ff'

%x

Hexadecimal integer

>>> "%x" % (0xff)

'ff'

>>> "%x" % (255)

'ff'

%o

Octal integer

>>> "%o" % (255)

377

>>> "%o" % (0377)

377

%u

Unsigned integer

>>> print "%u" % -2000

2147481648

>>> print "%u" % 2000

2000

%e

Float exponent

>>> print "%e" % (30000000L)

3.000000e+007

>>> "%5.2e" % (300000000L)

'3.00e+008'

%f

Float

>>> "check = %1.2f" % (3000)

 

 

'check = 3000.00'

 

 

 

 

 

>>> "payment = $%1.2f" % 3000

 

 

'payment = $3000.00'

%g

Float exponent

>>> "%3.3g" % 100

 

 

'100.'

 

 

 

 

 

>>> "%3.3g" % 1000000000000L

 

 

'10.e11'

 

 

 

 

 

>>> "%g" % 100

 

 

'100.'

%c

ASCII character

>>> "%c" % (97)

 

 

'a'

 

 

 

 

 

>>> "%c" % 97

 

 

'a'

 

 

 

 

 

>>> "%c" % (97)

 

 

'a'


Table 3–7 shows how flags can be used with the format directives to add leading zeroes or spaces to a formatted number. They should be inserted immediately after the %.

Table 3–7 Format Directive Flags

Flag

Description

Interactive Session

#

Forces octal to have a 0 prefix; forces hex to

>>> "%#x" % 0xff

have a 0x prefix

'0xff'

>>> "%#o" % 0377

'0ff'

+

Forces a positive number to have a sign

>>> "%+d" % 100

'+100'

-

Left justification (default is right)

>>> "%-5d, %-5d" % (10,10)

'10 , 10 '

" "

Precedes a positive number with a blank space

>>> "% d,% d" % (-10, 10)

'-100,10'

0

0 padding instead of spaces

>>> "%05d" % (100,)

'00100'


Advanced Topic: Using the %d, %i, %f, and %e Directives for Formatting Numbers

The % directives format numeric types: %i works with Integer; %f and %e work with Float with and without scientific notation, respectively.

>>> "%i, %f, %e" % (1000, 1000, 1000) 
'1000, 1000.000000, 10.000000e+002' 

Notice how awkward all of those zeroes look. You can limit the length of precision and neaten up your code like this:

>>> "%i, %2.2f, %2.2e" % (1000, 1000, 1000) 
'1000, 1000.00, 10.00e+002' 

The %2.2f directive tells Python to format the number as at least two characters and to cut the precision to two characters after the decimal point. This is useful for printing floating-point numbers that represent currency.

>>> "Your monthly payments are $%1.2f" % (payment) 
'Your monthly payments are $444.43' 

All % directives have the form %min.precision(type), where min is the minimum length of the field, precision is the length of the mantissa (the numbers on the right side of the decimal point), and type is the type of directive (e, f, i, or d). If the precision field is missing, the directive can take the form %min(type), so, for example, %5d ensures that a decimal number has at least 5 fields and %20f ensures that a floating-point number has at least 20.

Let's look at the use of these directives in an interactive session.

>>> "%5d" % (100,) 
' 100' 
>>> "%20f" % (100,) 
' 100.000000' 

Here's how to truncate the float's mantissa to 2 with %20.2f.

>>> "%20.2f" % (100,) 
' 100.00' 

The padding that precedes the directive is useful for printing rows and columns of data for reporting because it makes the printed output easy to read. This can be seen in the following example (from format.py):

     # Create two rows
row1 = (100, 10000, 20000, 50000, 6000, 6, 5) 
row2 = (1.0, 2L, 5, 2000, 56, 6.0, 7) 

      # 
      # Print out the rows without formatting 
print "here is an example of the columns not lining up" 
print ´row1´ + "\n" + ´row2´ 
print 
      # 
      # Create a format string that forces the number
      # to be at least 3 characters long to the left
      # and 2 characters to the right of the decimal point
format = "(%3.2e, %3.2e, %3.2e, %3.2e, " + \ "%3.2e, %3.2e, %3.2e)" 

      # 
      # Create a string for both rows
      # using the format operator
strRow1 = format % row1 
strRow2 = format % row2 
print "here is an example of the columns" + \ 
        " lining up using \%e" 

print strRow1 + "\n" + strRow2 
print 

      # Do it again this time with the %i and %d directive 
format1 = "(%6i, %6i, %6i, %6i, %6i, %6i, %6i)" 
format2 = "(%6d, %6d, %6d, %6d, %6d, %6d, %6d)" 
strRow1 = format1 % row1 
strRow2 = format2 % row2 
print "here is an example of the columns" + \ 
        " lining up using \%i and \%d" 

print strRow1 + "\n" + strRow2 
print 

here is an example of the columns not lining up 
(100, 10000, 20000, 50000, 6000, 6, 5) 
(1.0, 2L, 5, 2000, 56, 6.0, 7) 

here is an example of the columns lining up using \%e 
(1.00e+002, 1.00e+004, 2.00e+004, 5.00e+004, 6.00e+003, 6.00e+000, 5.00e+000) 
(1.00e+000, 2.00e+000, 5.00e+000, 2.00e+003, 5.60e+001, 6.00e+000, 7.00e+000) 

here is an example of the columns lining up using \%i and \%d 
( 100, 10000, 20000, 50000, 6000, 6, 5) 
(     1,         2,         5,   2000,     56, 6, 7) 

You can see that the %3.2e directive permits a number to take up only three spaces plus the exponential whereas %6d and %6i permit at least six spaces. Note that %i and %d do the same thing that %e does. Most C programmers are familiar with %d but may not be familiar with %i, which is a recent addition to that language.

String % Dictionary

Another useful Python feature for formatting strings is StringOperand % Dictio-naryOperand. This form allows you to customize and print named fields in the string. %(Income)d formats the value referenced by the Income key. Say, for example, that you have a dictionary like the one here:

Monica = { 
                 "Occupation": "Chef",
                 "Name" : "Monica", 
                 "Dating" : "Chandler",
                 "Income" : 40000 
                  } 

With %(Income)d, this is expressed as

>>> "%(Income)d" % Monica 
'40000' 

Now let's say you have three best friends, whom you define as dictionaries named Monica, Chandler, and Ross.

Monica = { 
                 "Occupation": "Chef",
                 "Name" : "Monica", 
                 "Dating" : "Chandler", 
                 "Income" : 40000 
                 } 

Ross = 
                               { 
                "Occupation": "Scientist Museum Dude",
                "Name" : "Ross", 
                "Dating" : "Rachael", 
                "Income" : 70000 
                } 

Chandler =              { 
                "Occupation": "Buyer",
                "Name" : "Chandler", 
                "Dating" : "Monica", 
                "Income" : 65000 
                } 

To write them a form letter, you can create a format string called message that uses all of the above dictionaries' keywords.

message = "%(Name)s, %(Occupation)s, %(Dating)s," \ 
                  " %(Income)2.2f" 

Notice that %(Income)2.2f formats this with a floating-point precision of 2, which is good for currency. The output is

Chandler, Buyer, Monica, 65000.00 
Ross, Scientist Museum Dude, Rachael, 70000.00 
Monica, Chef, Chandler, 40000.00 

You can then print each dictionary using the format string operator.

print message % Chandler 
print message % Ross 
print message % Monica 

To generate your form letter and print it out to the screen, you first create a format string called dialog.

dialog = """ 
Hi %(Name)s, 

How are you doing? How is %(Dating)s? 
Are you still seeing %(Dating)s? 

How is work at the office? 
I bet it is hard being a %(Occupation)s. 
I know I could not do it. 
""" 

Then you print out each dictionary using the dialog format string with the % format string operator.

print dialog % Ross 
print dialog % Chandler 
print dialog % Monica 

The output is

Hi Ross, 

How are you doing? How is Rachael? 
Are you still seeing Rachael? 

How is work at the office? 
I bet it is hard being a Scientist Museum Dude. 
I know I could not do it. 

Hi Chandler, 
How are you doing? How is Monica? 
Are you still seeing Monica? 

How is work at the office? 
I bet it is hard being a Buyer. 
I know I could not do it. 

Hi Monica, 
How are you doing? How is Chandler? 
Are you still seeing Chandler? 

How is work at the office? 
I bet it is hard being a Chef. 
I know I could not do it. 

%(Income)d is a useful, flexible feature. You just saw how much time it can save you in writing form letters. Imagine what it can do for writing reports.

  • + Share This
  • 🔖 Save To Your Account