簡體   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