返回要馈送到 string.format() 的参数元组 [英] Return a tuple of arguments to be fed to string.format()
本文介绍了返回要馈送到 string.format() 的参数元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我正在尝试在 Python 中获取一个方法来返回零、一个或两个字符串的列表以插入字符串格式化程序,然后将它们传递给字符串方法.我的代码看起来像这样:
Currently, I'm trying to get a method in Python to return a list of zero, one, or two strings to plug into a string formatter, and then pass them to the string method. My code looks something like this:
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()
?
When I try to run this code, the compiler gives an IndexError: tuple index out of range.
How should I structure my return value to pass it as an argument to .format()
?
推荐答案
print('Two pair, {0}s and {1}s'.format(*cards))
你只缺了星星 :D
这篇关于返回要馈送到 string.format() 的参数元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文