在 Python3 中,我想编写一条通向列表中所有项目(在本例中为晚餐客人的姓名)的一揽子消息,而不必单独打印每条消息。
例如,如果列表是:
guest_list = ['John', 'Joe', 'Jack']
我希望它使用每个人的名字在下面打印这一行,而不必单独打印消息 3 次:
print("Hello, " + *name of guest from the list above here* + "! We have found a bigger table!")
期望的结果:
Hello, John! We have found a bigger table!
Hello, Joe! We have found a bigger table!
Hello, Jack! We have found a bigger table!
这可能吗?如果是这样,如何?感谢提供的任何帮助!
最佳答案
如果你只想做一个print
:
guest_list = ['John', 'Joe', 'Jack']
result_str = ['Hello {}! We have found a bigger table!'.format(guest) for guest in guest_list]
print(result_str.join('\n'))
关于Python:如何在不单独打印每条消息的情况下向列表中的项目写入消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53400634/