在不同的行中打印每个列表元素,在每个元素前面重复一个字符串Python

2024-06-16 09:40:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在列表的每个元素前面打印一个字符串,然后在新行中打印:

示例:

    test = ["aaa", "bee", "cee"]
    print("hello, %s" % "\n".join(storageVolume))

我从中得到的是:

^{pr2}$

我想要的是:

    hello, aaa
    hello, bee
    hello, cee

感谢任何帮助。在


Tags: 字符串test元素示例hello列表beeprint
3条回答
In [10]: test = ["aaa", "bee", "cee"]

In [11]: print "\n".join("hello, "+x for x in test)
hello, aaa
hello, bee
hello, cee

或者:

^{pr2}$
for x in test:
    print "Hello, {0}".format(x) 
In [51]: test = ["aaa", "bee", "cee"]

In [52]: for elem in test:
   ....:     print "hello,", elem
   ....:
hello, aaa
hello, bee
hello, cee

相关问题 更多 >