0

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

例子:

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

我从中得到的是:

    hello, aaa
    bee
    cee

我想要的是:

    hello, aaa
    hello, bee
    hello, cee

任何帮助表示赞赏。

4

4 回答 4

3
for x in test:
    print "Hello, {0}".format(x) 
于 2012-09-26T11:50:10.563 回答
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
于 2012-09-26T11:50:26.640 回答
1
In [51]: test = ["aaa", "bee", "cee"]

In [52]: for elem in test:
   ....:     print "hello,", elem
   ....:
hello, aaa
hello, bee
hello, cee
于 2012-09-26T11:49:43.000 回答
0
     for i in range(len(test)):
        print "Hello, "+(test[i]);
于 2012-09-26T11:55:37.637 回答