首页 > 解决方案 > Python3 - 打印字符串和列表

问题描述

是否可以将下面的代码写得更短,一行?

print("Start line ", end="")
print(*list, sep=", ", end="")
print(" end of line.")

标签: python-3.x

解决方案


print您可以在以下帮助下尝试使用单个呼叫join

list = ['one', 'two', 'three']
print("Start line [" + ", ".join(list) + "] end of line.", end="")

Start line [one, two, three] end of line.

推荐阅读