num = 1 flnum = 2.6 print("The integer is", num, "and the float is", flnum)

The integer is 1 and the float is 2.6

print("Here is one print statement") print("And here is another")

Here is one print statement And here is another

print("Here is one print statement", end=’ ’) print("And here is another")

Here is one print statement And here is another

print("$", 1.39)

$ 1.39

print("$%f" % (1.39))

$1.390000

One problem with the above—too many decimal places. Let’s restrict it to 2 decimal places by saying instead:

This printsprint("$%.2f" % (1.39))

$1.39

import math print("The value of pi is %.7f" % ( math .pi ))

The value of pi is 3.1415927

for i in range(1, 5): print("%.2f" % ( math.pi * i))

3.14 6.28 9.42 12.57

That doesn’t line up too well! We really want the numbers to the *left* of the decimal point to take up the same amount of room so the decimal points line up. So, we want to line the numbers up to the right. To do this, we first figure out how many characters (including the decimal point) we want the numbers to take up. That’s 5 (because “12.57” takes 5 spaces to print and the rest take 4). So, we use the following format string:

When you make this change to the above"%5.2f"

3.14 6.28 9.42 12.57

Now let’s say we want to print several arguments. You do it this way:

givesprint("%d + %d = %d" % (2, 2, 2 + 2))

2 + 2 = 4

print('%(language)s has %(Q)03d quote types.' % {"Q":2, 'language' : "Python"})

Python has 002 quote types.

You can provide a format specification to control how the arguments to `format`
are printed. For example:

You getimport math print("The value of pi is {0:.7f}".format(math.pi))

The value of pi is 3.1415927

You can also control positioning within the field:

producesprint("This prints to the left in the field: '{0:<6}'".format('text')) print("This prints to the right in the field: '{0:>6}'".format('text')) print("This prints in the center of the field: '{0:^6}'".format('text'))

This prints to the left in the field: 'text ' This prints to the right in the field: ' text' This prints in the center of the field: ' text '

In the first method, the string in quotes is called the *format string* and the parenthesized list following the “%” are the *values*. The values are substituted into the format string, resulting in a new string. To see this, type the following to the IDLE interpreter:

and you will seeimport math x = "2 * %f = %f" % ( math.pi, 2* math.pi) print(x)

2 * 3.141593 = 6.283185

The same thing is true for the `format` method. Again, to see this, type the following to the IDLE interpreter:

and you will again seeimport math x = "2 * {0:.6f} = {1:.6f}".format(math.pi, 2*math.pi) print(x)

2 * 3.141593 = 6.283185

- Actually, if there is only one argument to be printed, and that argument is a
*number*, you can omit the parentheses. But if it’s an expression, leaving the parentheses out could cause unexpected results, so it’s better to put them in. - Here’s a good example of where the argument requires the parentheses even though only one number is being printed. Try leaving them out and see what happens.

You can also obtain a PDF version of this. | Version of March 29, 2014 at 10:29PM |