使用格式打印运行时可能包含未知数量变量的列表?

use*_*776 1 python format

sample_list当我知道可以容纳 4 件物品时,以下方法有效。

sample_list = ['cat', 'dog', 'bunny', 'pig']
print("Your list of animals are: {}, {}, {} and {}".format(*sample_list))
Run Code Online (Sandbox Code Playgroud)

sample_list如果我不知道运行时将包含的项目数,如何格式化字符串?这意味着我无法在设计时输入适当数量的括号占位符。

C.N*_*ivs 5

我只想用join

sample_list = ['cat', 'dog', 'bunny', 'pig']
printstr = '%s, and %s' % (', '.join(sample_list[:-1]), str(sample_list[-1]))
print("Your list of animals are: %s" % printstr)
Run Code Online (Sandbox Code Playgroud)