Outils pour utilisateurs

Outils du site


issue78:tuto_python

Ceci est une ancienne révision du document !


Welcome back. It’s hard to imagine that it’s been 4 years since I began this series. I thought that I’d shelve the media manager project for a bit and return to some basics of Python programming.

This month, I’ll revisit the print command. It’s one of the most used (at least in my programming) function that never seems to get the detail it deserves. There is a lot of things you can do with it outside of the standard ‘%s %d’.

Since the print function syntax is different between Python 2.x and 3.x, we’ll look at them separately. Remember, however, you can use the 3.x syntax in Python 2.7. Most everything I present this month will be done from the interactive shell. You can follow along as we go. The code will look like this:

a = “Hello Python”
print(“String a is %s” % a)

and the output will be in bold, like this:

String a is Hello Python

Python 2.x

Of course you remember the simple syntax for the print function in 2.x uses the variable substitution of %s or %d for simple strings or decimals. But many other formatting options are available. For example, if you need to format a number with leading zeros, you can do it this way:

print(“Your value is %03d” % 4)

Your value is 004

In this case, we use the ‘%03d’ formatting command to say, “Display the number to a width of 3 characters and if needed, left pad with zeros”.

pi = 3.14159
print('PI = %5.3f.' % pi)

PI = 3.142.

Here we use the float formatting option. The ‘%5.3f’ says to produce an output with a total width of five and three decimal places. Notice that the decimal point takes up one of the places of the total width.

One other thing that you might not realize is that you can use the keys of a dictionary as part of the format command.

info = {“FName”:“Fred”,“LName”:“Farkel”,“City”:“Denver”}
print('Greetings %(FName)s %(LName)s of %(City)s!' % info)

Greetings Fred Farkel of Denver!

The following table shows the various possible substitution keys and their meanings.

Python 3.x

With Python 3.x, we have many more options (remember we can use these in Python 2.7) when it comes to the print function.

To refresh your memory, here’s a simple example of the 3.x print function.

print('{0} {1}'.format(“Hello”,“Python”))

Hello Python

print(“Python is {0} cool!”.format(“WAY”))

Python is WAY cool!

The replacement fields are enclosed within curly brackets “{“ “}”. Anything outside of these are considered a literal and will be printed as is. In the first example, we have numbered the replacement fields 0 and 1. That tells Python to take the first (0) value and put it into the field {0} and so on. However, you don’t have to use any numbers at all. Using this option causes the first value to be places in the first set of brackets and so on.

print(“This version of {} is {}”.format(“Python”,“3.3.2”))

This version of Python is 3.3.2

As they say on the TV ads, “BUT WAIT… THERE’S MORE”. If we wanted to do some inline formatting, we have the following options.

:<x Left align with a width of x :>x Right align with a width of x :^x Center align with a width of x

Here is an example:

print(“|{:<20}|”.format(“Left”))

|Left |

print(“|{:>20}|”.format(“Right”))

| Right|

print(“|{:^20}|”.format(“Center”))

| Center |

You can even specify a fill character along with the justification/width.

print(“{:*>10}”.format(321.40))

*321.4

If you need to format a date/time output, you can do something like this:

d = datetime.datetime(2013,10,9,10,45,1)
print(“{:%m/%d/%y}”.format(d))

10/09/13

print(“{:%H:%M:%S}”.format(d))

10:45:01

Printing thousands separator using a comma (or any other character) is simple.

print(“This is a big number {:,}”.format(7219219281))

This is a big number 7,219,219,281

Well, that should give you enough food for thought for this month. I’ll see you at the start of the 5th year.

issue78/tuto_python.1386415078.txt.gz · Dernière modification : 2013/12/07 12:17 de andre_domenech