Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/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 3.x Python-使用动态值生成字符串_Python 3.x - Fatal编程技术网

Python 3.x Python-使用动态值生成字符串

Python 3.x Python-使用动态值生成字符串,python-3.x,Python 3.x,我正在尝试创建一个多行字符串,其中也设置了动态行数和值 我有: string = str( "hello \n"+ "foo %f \n" % (-1) + for i in range(5): n = i + 1 "bar %f \n" % (n) + "END" ) print(str(string)) 然而,这给了我一个语法错误,但我要寻找的输出是这样的: hello foo -1 bar 0 bar 2 bar 3 bar 4 bar 5 bar 6 END 您不需要

我正在尝试创建一个多行字符串,其中也设置了动态行数和值

我有:

string = str(
"hello \n"+
"foo %f \n" % (-1) +
for i in range(5):
    n = i + 1
    "bar %f \n" % (n) +
"END"
)
print(str(string))
然而,这给了我一个语法错误,但我要寻找的输出是这样的:

hello
foo -1
bar 0
bar 2
bar 3
bar 4
bar 5
bar 6
END

您不需要
str
,因为您的值已经是一个字符串。 语法错误是
打印前结尾的右括号

您可以尝试以下代码:

s=“”你好
富-1
"""
对于范围(5)中的i:
s+=''条{value}
“”格式(值=i+1)
s+=“结束”
印刷品