PrancingCrabulon
PrancingCrabulon

Reputation: 412

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()

Upvotes: 2

Views: 146

Answers (1)

Håken Lid
Håken Lid

Reputation: 23064

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")  

Upvotes: 4

Related Questions