Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 什么是打印(f“…”)_Python - Fatal编程技术网

Python 什么是打印(f“…”)

Python 什么是打印(f“…”),python,Python,我正在阅读一个python脚本,它接收XML文件的输入并输出XML文件。但是,我不理解打印语法。有人能解释一下print(f“…”中的f是做什么的吗 args = parser.parser_args() print(f"Input directory: {args.input_directory}") print(f"Output directory: {args.output_directory}") f字符串也称为文本字符串,用于在字符串中插入变量并使其成为字符串的一部分,而不是这样做

我正在阅读一个python脚本,它接收XML文件的输入并输出XML文件。但是,我不理解打印语法。有人能解释一下
print(f“…”
中的
f
是做什么的吗

args = parser.parser_args()

print(f"Input directory: {args.input_directory}")
print(f"Output directory: {args.output_directory}")

f字符串也称为文本字符串,用于在字符串中插入变量并使其成为字符串的一部分,而不是这样做

x = 12
y = 10

word_string = x + ' plus ' + y + 'equals: ' + (x+y)
相反,你可以这样做

x = 12
y = 10

word_string = f'{x} plus {y} equals: {x+y}'
output: 12 plus 10 equals: 22
这也将有助于增加间距,因为它将完全按照字符串所写的方式来执行
f
的意思,并且它在
python3.6
中是新的


格式化字符串文字或f-string是 前缀为
'f'
'f'
。这些字符串可能包含替换 字段,这些字段是由大括号分隔的表达式
{}
。虽然 其他字符串文字总是有一个常量值,即格式化字符串 表达式实际上是在运行时计算的


格式化字符串文字的一些示例:

>>> name = "Fred"
>>> f"He said his name is {name}."
"He said his name is Fred."

>>> name = "Fred"
>>> f"He said his name is {name!r}."
"He said his name is Fred."

>>> f"He said his name is {repr(name)}." # repr() is equivalent to !r
"He said his name is Fred."

>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}" # nested fields
result: 12.35

>>> today = datetime(year=2017, month=1, day=27)
>>> f"{today:%B %d, %Y}" # using date format specifier
January 27, 2017

>>> number = 1024
>>> f"{number:#0x}" # using integer format specifier
0x400

在Python 3.6中,引入了f字符串(PEP 498)。简而言之,这是一种格式化字符串的方法,它更具可读性和速度

例如:

agent_name='jamesbond'
杀戮计数=9
#老路
打印({0}杀死了{1}个敌人。格式(代理名称,杀死计数))
#f-弦方式
打印(f'{agent_name}杀死了{kill_count}个敌人')
字符串前面的
f
f
告诉Python查看{}中的值,如果存在,用变量值替换它们。最棒的是你可以在{}中做一些很酷的事情,例如,
{kill_count*100}

阅读资料:

print("Input directory: {}".format(args.input_directory))
print("Output directory: {}".format(args.output_directory))
print("Input directory: "+args.input_directory)
print("Output directory: "+args.output_directory)
这也和

print("Input directory: {}".format(args.input_directory))
print("Output directory: {}".format(args.output_directory))
print("Input directory: "+args.input_directory)
print("Output directory: "+args.output_directory)

。此语法仅适用于python 3.6以后的版本。它们是f字符串。这是Python3.6中引入的一个新概念。请注意,严格来说,这三种技术并不等同。它们各自具有不同的性能特征,处理非字符串参数的方式也各不相同。第二个
word\u string