繁体   English   中英

为什么print()向输出添加多余的空格?

[英]Why does print() add extra whitespace to the output?

下面是我的代码,在打印我创建的第二个文件时,我会缩进。 不明白为什么。 由于我正在学习有关我的简单代码的任何其他注释,因此非常欢迎,我们将乐意接受。 谢谢!

from sys import argv
script, file_name = argv
file_object = open(file_name)
print(file_object.read())
file_object.close()
second_file_name = input("Enter the second file name: \r\n>> ")
second_file_object = open(second_file_name + 'txt', 'w+')
second_file_object.write(input("Now write to your file: \r\n>> "))
second_file_object.seek(0)
print("printing the second file object: \r\n", second_file_object.read())  
# why is the file content indented when printed?
second_file_object.close()

默认情况下, print将在每个参数之间添加一个空格。

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

如果更改分隔符,则可以删除不需要的空格字符。

print("printing the second file object:", second_file_object.read(), sep="\n")  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM