Printing in Columns

excelcolumns

Using the % Operator in Strings

In Python, you can include a variable within a string for output using the % operator.  The % symbol along with a type indicator (and possibly length and justification information) acts as a placeholder in the string.  The % is also used as an operator to indicate the values that should be substituted for the placeholders.

That’s a mouthful.  Let me show you an example:

x = 2
name = "Stanley"
print('The value of x is %d and the value of name is %s.' % (x, name))

The output of this code will be:

The value of x is 2 and the value of name is Stanley.

Python puts the 2 in where the %d is located and Stanley where the %s is located.  This is a more efficient method for including values of variable in strings than using the + operator in many cases.

The real power of the % operator comes when you want to format output into justified columns.  The following code shows examples of using fixed width placeholders and justified placeholders to organize output.

# print formatted tables
# Example of right-justified columns of size 5, 8, and 12.
for i in range(10):
    print("%5d %8d %12d") % (i,i*4,2*i-8)

# Blank Line
print('')

# Example of left-justified columns of size 5, 8, and 12.
for i in range(10):
    print ('%-5d %-8d %-12d') % (i,i*4,2*i-8)

Using numbers and negative signs with the % placeholders allows us to create columns in our output.  Give it a try.

So we use %d to display integers and %s to display strings.  We can also display decimal numbers (floating point decimals) using %f.  And, the % operator can even round off a decimal number to a specified number of  places.  For example, %5.2f will display the number in a column five spaces wide rounded off to two decimal places.  Run the example code below and observe how the %f placeholder functions.

# Columns of width 9 with a header row.
print('%-9s  %-9s  %-9s') % ('Single','Double','Triple')
x = 2.50
while x < 9:
    print('$%-9.2f $%-9.2f $%-9.2f') % (x,2*x,3*x)
    x = x + 0.5

Click this link to find more information on string formatting operations.

Your Assignment

So today, we are just going to briefly practice this formatted column output by writing a quick program.  Here are the specifics:

Write a program that creates a three-column table displaying the calculated weekly and yearly salaries for hourly rates from $7.50 an hour to $15.50 an hour in $0.50 intervals (like the code above).  You can assume 50 weeks in a year (52 minus a two-week vacation) and a 40-hour work week.  Make columns of width 9.  Sample output with the first column and row completed is show below:

Hourly     Weekly     Yearly   
$7.50      $300.00    $15000.00 
$8.00 
$8.50 
$9.00 
$9.50 
$10.00  
$10.50    
$11.00    
$11.50   
$12.00  
$12.50   
$13.00  
$13.50   
$14.00   
$14.50    
$15.00    
$15.50

When your program is working properly, please use Microsoft Word to prepare a one-page document to turn in, which includes a description, your program code, and the output of the program.  Just copy and paste the text into the document from IDLE. Include your name at the top of the document and a section for the description of the program, the program code section, and the output section as shown in this sample document.

Before printing, please highlight the program code and output in Word and change the font to Courier New and make the font size 12, like the sections in the sample document. Print your one-page document and turn it in.