Python sep =","但只想第二次向前

时间:2015-07-11 00:05:12

标签: python

所以我正在制作基础文本游戏,因为我正在学习python。 当我提供选择时,我试图从列表中打印选项,我有这些代码行:

hubchoices = ["fight","shop","upgrade stats"]

print("You Can,"hubchoices[0].capitalize(),,hubchoices[1].capitalize(),hubchoices[2].capitalize(), sep= ", ")

我收到的输出是:

You Can, Fight, Shop, Upgrade stats

有没有办法让sep =","只有在第一个逗号之后才能得到更像这样的东西:

You Can Fight, Shop, Upgrade stats

(也是一种用.capitalize()来大写每个单词的第一个字母的方法)

2 个答案:

答案 0 :(得分:3)

尝试str.join

print("You Can %s" % ','.join([c.capitalize() for c in hubchoices]))

答案 1 :(得分:0)

尝试字符串格式化。

hubchoices = ["fight","shop","upgrade stats"]
'You can: {}, {}'.format(*map(str.capitalize, hubchoices[1:]))

结果:

'You can: Shop, Upgrade stats'