Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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/2/cmake/2.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将列表打印为字符串_Python_String_List_Printf_Concatenation - Fatal编程技术网

使用列表格式| Python将列表打印为字符串

使用列表格式| Python将列表打印为字符串,python,string,list,printf,concatenation,Python,String,List,Printf,Concatenation,尝试打印带有字符串的列表,并将其强制转换为str(),会引发以下错误: doc = "Title" count_scoring = [1, 2, 3] print("\"" + doc + "\" = " + count_scoring) 错误(两次尝试): 期望输出: "doc" = [1, 2, 3] 当我运行代码时,print(“\”+doc+“\”=“+str(计数评分))对我有效,没

尝试打印带有字符串的列表,并将其强制转换为str(),会引发以下错误:

doc = "Title"
count_scoring = [1, 2, 3]
print("\"" + doc + "\" = " + count_scoring)
错误(两次尝试):

期望输出:

"doc" = [1, 2, 3]

当我运行代码时,
print(“\”+doc+“\”=“+str(计数评分))
对我有效,没有出现错误,因此您可能还有其他问题。但是,如果确定不需要,可以使用字符串格式:

print(f“{doc}\={count\u scoring}”)
另一种方法是使用
,“join()
。这种方式要长得多,但仍然是一种选择:

print(“\”+doc+“\”=“+”[“+”,”.join(列表(映射(str,计数评分)))+“]))
开始是一样的。然后,我们做
列表(map(str,count\u scoring))
。这样做的目的是将列表中的所有值转换为字符串。然后,我们使用
,“join
将所有这些值相加,得到如下结果:
“1,2,3”
。最后,我们在开头和结尾添加“[”和“]”,使其看起来像一个列表

doc = "Title"
count_scoring = [1, 2, 3]
print(f'"{doc}" = {count_scoring}')


格式化字符串只能在Python 3.x.x中使用。

打印(“\doc\”=“+str(count\u scoring))
。doc不是一个变量,它是一个常量或右值,你可以调用它。你也可以使用
来删除外部字符串,这样你就可以在里面使用unescaped
。对不起,它是一个变量。我没有更新帖子以包含此内容。您想要的输出是
“doc”=[1,2,3]
还是
“Title”=[1,2,3]
?第二个代码块有效。-。。不要理解你的问题-它只打印“Title”,这是Python版本。刚刚在一个包含Python3的oline IDE上试用过,它正在工作。泰姆
"doc" = [1, 2, 3]
doc = "Title"
count_scoring = [1, 2, 3]
print(f'"{doc}" = {count_scoring}')