在python 2.7中将列表元素打印为字符串

时间:2014-10-29 00:01:05

标签: python-2.7

我有一个包含字母和数字对的字符串列表,例如     a = [" B8"," C1"," B4"," A3"]

我希望能够使用此列表并将其组合(不带括号,逗号或引号)和基本字符串"您有"在打印声明中告诉用户     你有B8 C1 B4 A3

使用.join()和.append()方法似乎无法做到这一点。

3 个答案:

答案 0 :(得分:1)

str.formatstr.join将完全符合您的要求:

a = ["B8", "C1", "B4", "A3"]
print("You have {}".format(" ".join(a)))

In [6]: a=["B8", "C1", "B4", "A3"]
In [7]: print("You have {}".format(" ".join(a)))
You have B8 C1 B4 A3

答案 1 :(得分:0)

a=["B8", "C1", "B4", "A3"]
print "You Have " + " ".join(a)

答案 2 :(得分:0)

a=["B8", "C1", "B4", "A3"]
for item in a:
    print 'You have',item

希望这有用。