如何在一行中打印出字符串和列表-python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25733737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to print out a string and list in one line-python
提问by user12551
a=[1,2,3]
print "the list is :%"%(a)
I want to print out one line like this: the list is:[1,2,3]I can not make it within one line and I have to do in this way:
我想像这样打印出一行:the list is:[1,2,3]我不能在一行中打印出来,我必须这样做:
print " the list is :%"
print a
I am wondering whether I can print out something that combine with string formatting and a list in ONE line.
我想知道是否可以在一行中打印出与字符串格式和列表相结合的内容。
回答by user590028
Try this:
尝试这个:
a = [1,2,3]
print("the list is: %s" % a)
回答by Roger Fan
回答by Newb
At least in Python 2.7.4., this will work:
至少在 Python 2.7.4 中,这将起作用:
print " the list is " + str(a)
回答by moto
The simplest way is what CodeHard_or_HardCode said in the comments. For Python 3 it would be:
最简单的方法就是 CodeHard_or_HardCode 在评论中所说的。对于 Python 3,它将是:
a=[1,2,3]
print('This is a list', a)
This is a list [1, 2, 3]
回答by Sareesh-Hassan
print(f"the list is: {[1,2,3]}")
you will need to use python 3.7.1 or higher I believe, as this is when they added string interpolation
我相信你需要使用 python 3.7.1 或更高版本,因为这是他们添加字符串插值的时候

