Formatted output of the Python 3.2 official documentation Tutorial

Source: Internet
Author: User

There are many ways to output a program, and the data can be printed or written to a file for future use in the form that people read. These issues are discussed in this chapter.

5.1 Formatted output

We have now contacted two output value modes: Expression statements and print () functions. (The third is the use of the Write () method in the file object; The standard file output can refer to the Sys.stdout library file)

Often you want to control the output formatted data instead of simply separating the characters with a space. Here are two ways to format your output data. The first way is to handle all the strings yourself, using the Shard or link operation in the string you can create any character you want. The standard module string contains some useful actions to populate the string with the width of the specified column, which is discussed later. The second approach is to use the Str.format () method.

The string module contains a template class that provides an alternative way to convert a value into a string form. Of course there is a question: How do you convert a value to a string? Fortunately, Python has provided a variety of methods for converting any value into a string: passing the value to method repr () or STR ();

The STR () function is used to return a more convenient form of human reading. The Repr () method is used to produce an interpreter in a convenient form. (if there are no equal statements, a syntaxerror will be generated). The STR () return value is the same as the repr () return value for a special form that does not provide a convenient way for people to read. For more values such as numbers or structures similar to lists and dictionaries, both methods are used to produce the same representation. In particular, the string has two different performance implementations.

For example:

>>> s = ' Hello, world. '

>>> Str (s)

' Hello, world. '

>>> Repr (s)

"' Hello, world. '"

>>> Str (1/7)

' 0.14285714285714285 '

>>> x = 10*3.25

>>> y = 200*200

>>> s = ' The value of x ' + repr (x) + ', and y is ' + repr (y) + ' ... '

>>> print (s)

The value of x is 32.5, and Y is 40000 ...

>>> # The REPR () of a string adds string quotes and backslashes:

... hello = ' Hello, world\n '

>>> hellos = repr (hello)

>>> Print (hellos)

' Hello, world\n '

>>> # The argument to Repr () May is any Python object:

... repr ((x, y, (' spam ', ' eggs '))

"(32.5, 40000, (' spam ', ' eggs ')")

There are two ways to output tables of squares and cubes:

>>> for x in range (1, 11):

... print (repr (x). Rjust (2), repr (x*x). Rjust (3), end= "")

... # with ' End ' on previous line

... print (repr (x*x*x). Rjust (4))

...

1 1 1

2 4 8

3 9 27

4 16 64

5 25 125

6 36 216

7 49 343

8 64 512

9 81 729

10 100 1000

>>> for x in range (1, 11):

... print (' {0:2d} {1:3d} {2:4d} '. Format (x, X*x, x*x*x))

...

1 1 1

2 4 8

3 9 27

4 16 64

5 25 125

6 36 216

7 49 343

8 64 512

9 81 729

10 100 1000

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.