【问题标题】:white space added in the beginning of new line when printing in new line在新行打印时在新行开头添加空白
【发布时间】:2018-10-22 06:45:10
【问题描述】:

当我尝试使用 python 3 在多行上打印任何数据时,除了第一行之外,所有行的开头都会添加一个空格。例如:

[in] print('a','\n','b','\n','c')

输出将是:

a
 b
 c

但我想要的输出是:

a
b
c

到目前为止,我只能通过执行三个打印命令来做到这一点。有人有什么想法吗?

【问题讨论】:

    标签: python-3.x printing output whitespace


    【解决方案1】:

    来自docs

    print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

    将对象打印到文本流文件,以 sep 分隔,后跟 end。 sep、end、file 和 flush,如果存在,必须作为关键字给出 论据。

    调用 print('a', '\n', 'b') 将打印这三个项目中的每一个,并在它们之间添加一个空格,这就是您所看到的。

    您可以更改分隔符参数以获得您想要的:

    print('a', 'b', sep='\n')
    

    另请参阅format 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多