python - 返回要馈送到 string.format() 的参数元组

标签 python string-formatting tuples

目前,我正在尝试在 Python 中获取一个方法来返回零、一个或两个字符串的列表以插入字符串格式化程序,然后将它们传递给字符串方法。我的代码如下所示:

class PairEvaluator(HandEvaluator):
  def returnArbitrary(self):
    return ('ace', 'king')

pe = PairEvaluator()
cards = pe.returnArbitrary()
print('Two pair, {0}s and {1}s'.format(cards))

当我尝试运行此代码时,编译器会给出 IndexError: tuple index out of range。
我应该如何构造我的返回值以将其作为参数传递给 .format()

最佳答案

print('Two pair, {0}s and {1}s'.format(*cards))

你只缺少星星 :D

关于python - 返回要馈送到 string.format() 的参数元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/539066/

相关文章:

即时进行 Python 字符串格式化

python - 从元组+一些数字中选择一个随机数

python - 在 Pylons 或 Python 中销毁 session

python - 想输入诸如 “01”和 “07”之类的整数,但出现 “invalid literal”错误。总体方案是RLE压缩机

python - Django 无法在数据库中保存表单

python - 在不复制变量的情况下重用 %s 占位符?

python - 将 Python v2 函数移植到 v3

c++ - 限制固定输出的尾随零数

python - 将元组索引拆分为多索引 Pandas 数据帧 - 使用 python

c++ - 从类型元组到通过调用模板函数返回的值数组