简体   繁体   中英

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

Below is my code, when printing the second file i create, i get an indent. Don't see why. Since I'm learning any other comments on my simple code are wellcome, and will be taken gladly. Thanks!

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 will add a single space between each argument by default.

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

If you change the separator, you can remove the space character you don't want.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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