【问题标题】:Python3 - print string and listPython3 - 打印字符串和列表
【发布时间】:2018-12-10 13:38:30
【问题描述】:

下面的代码可以写成更短的一行吗?

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

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    您可以尝试在join 的帮助下使用单个print 调用:

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

    【讨论】:

      【解决方案2】:

      可能会更短一点。

      print(f'Start Line {lst} End of line')
      

      【讨论】:

        猜你喜欢
        • 2017-02-10
        • 2018-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-11
        • 1970-01-01
        • 1970-01-01
        • 2020-08-26
        相关资源
        最近更新 更多