Python 教程 10: 输入输出 (Input and Output)
Input and Output
There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use. This chapter will discuss some of the possibilities.
Fancier Output Formatting
So far we’ve encountered two ways of writing values: expression statements and the print() function. (A third way is using the write() method of file objects; the standard output file can be referenced as sys.stdout. See the Library Reference for more information on this.)
Often you’ll want more control over the formatting of your output than simply printing space-separated values. There are several ways to format output.
- To use formatted string literals, begin a string with
forFbefore the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between{and}characters that can refer to variables or literal values.
year = 2016
event = 'Referendum'
f'Results of the {year} {event}'
'Results of the 2016 Referendum'
- The
str.format()method of strings requires more manual effort. You’ll still use{and}to mark where a variable will be substituted and can provide detailed formatting directives, but you’ll also need to provide the information to be formatted.