使用在每个元素Python之前重复的字符串打印不同行中的每个列表元素

时间:2012-09-26 11:47:37

标签: python python-2.7

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

示例:

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

我从中得到的是:

    hello, aaa
    bee
    cee

我想要的是:

    hello, aaa
    hello, bee
    hello, cee

感谢任何帮助。

4 个答案:

答案 0 :(得分:3)

for x in test:
    print "Hello, {0}".format(x) 

答案 1 :(得分:2)

In [10]: test = ["aaa", "bee", "cee"]

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

或:

In [13]: print "\n".join("hello,  {0}".format(x) for x in test)
hello,  aaa
hello,  bee
hello,  cee

答案 2 :(得分:1)

In [51]: test = ["aaa", "bee", "cee"]

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

答案 3 :(得分:0)

     for i in range(len(test)):
        print "Hello, "+(test[i]);