Numeric Display Formats

Notice that we used a print operation in the last of the preceding examples. Without the print, you’ll see something that may look a bit odd at first glance:

>>> b / (2.0 + a)           # Auto echo output: more digits
0.80000000000000004

>>> print(b / (2.0 + a))    # print rounds off digits
0.8

广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元

The full story behind this odd result has to do with the limitations of floating-point hardware and its inability to exactly represent some values in a limited number of bits. Because computer architecture is well beyond this book’s scope, though, we’ll finesse this by saying that all of the digits in the first output are really there in your computer’s floating-point hardware—it’s just that you’re not accustomed to seeing them. In fact, this is really just a display issue—the interactive prompt’s automatic result echo shows more digits than the print statement. If you don’t want to see all the digits, use print; as the sidebar str and repr Display Formats will explain, you’ll get a user-friendly display.

Note, however, that not all values have so many digits to display:

>>> 1 / 2.0
0.5

and that there are more ways to display the bits of a number inside your computer than using print and automatic echoes:

>>> num = 1 / 3.0
>>> num                      # Echoes
0.33333333333333331
>>> print(num)               # print rounds
0.333333333333

>>> '%e' % num               # String formatting expression
'3.333333e-001'
>>> '%4.2f' % num            # Alternative floating-point format
'0.33'
>>> '{0:4.2f}'.format(num)   # String formatting method (Python 2.6 and 3.0)
'0.33'

The last three of these expressions employ string formatting, a tool that allows for format flexibility, which we will explore in the upcoming chapter on strings (Chapter 7). Its results are strings that are typically printed to displays or reports.


str and repr Display Formats

Technically, the difference between default interactive echoes and print corresponds to the difference between the built-in repr and str functions:

>>> num = 1 / 3
>>> repr(num)              # Used by echoes: as-code form
'0.33333333333333331'
>>> str(num)               # Used by print: user-friendly form
'0.333333333333'

Both of these convert arbitrary objects to their string representations: repr (and the default interactive echo) produces results that look as though they were code; str (and the print operation) converts to a typically more user-friendly format if available. Some objects have both—a str for general use, and a repr with extra details. This notion will resurface when we study both strings and operator overloading in classes, and you’ll find more on these built-ins in general later in the book.

Besides providing print strings for arbitrary objects, the str built-in is also the name of the string data type and may be called with an encoding name to decode a Unicode string from a byte string. We’ll study the latter advanced role in Chapter 36 of this book.