- 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.
本章将讨论一些可能性。
7.1. Fancier Output Formatting 更高级的输出格式
So far we’ve encountered two ways of writing values:
到目前为止,我们遇到了两种写入值的方法:
expression statements and the print() function.
表达式语句和print()函数。
(A third way is using the write() method of file objects;
第三种方法是使用file对象的write()方法;
the standard output file can be referenced as sys.stdout.
标准输出文件可以作为 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.
有(以下)几种格式化输出的方法。
1、
To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark.
要使用 格式化字符串字面值 ,请在字符串的开始引号或三引号之前加上一个 f 或 F 。
Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values.
在此字符串中,你可以在 { 和 } 字符之间写一个可以引用的变量或字面量的 Python 表达式。
>>> year = 2016
# 全民公投事件
>>> event = 'Referendum'
>>> f'Results of the {year} {event}'
'Results of the 2016 Referendum'
'2016年全民公投的结果'
year = 2020
event = '新冠病毒'
f'{year}年爆发了{event}'
'2020年爆发了新冠病毒'
网友评论