a=[1,2,3]
print "the list is ?%"%(a)
Run Code Online (Sandbox Code Playgroud)
我想打印出这样一行:the list is?[1,2,3]
我不能在一行内完成,我必须这样做:
print " the list is :%"
print a
Run Code Online (Sandbox Code Playgroud)
我想知道我是否可以打印出与字符串格式和一行列表相结合的内容.
mot*_*oto 11
最简单的方法是CodeHard_or_HardCode在评论中所说的内容.对于Python 3,它将是:
a=[1,2,3]
print('This is a list', a)
This is a list [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
我建议使用 stringformat方法,因为它优于旧%语法。
print("the list is: {}".format(a))
Run Code Online (Sandbox Code Playgroud)
尝试这个:
a = [1,2,3]
print("the list is: %s" % a)
Run Code Online (Sandbox Code Playgroud)