我有一个包含字母和数字对的字符串列表,例如 a = [" B8"," C1"," B4"," A3"]
我希望能够使用此列表并将其组合(不带括号,逗号或引号)和基本字符串"您有"在打印声明中告诉用户 你有B8 C1 B4 A3
使用.join()和.append()方法似乎无法做到这一点。
答案 0 :(得分:1)
str.format和str.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
希望这有用。