akira
akira

Reputation: 531

print only one single empty line

I am reading files in a folder in a python. I want print the each file content separate by a single empty line.

So, after the for loop I am adding print("\n") which adding two empty lines of each file content. How can I resolve this problem?

Upvotes: 0

Views: 99

Answers (5)

Synedraacus
Synedraacus

Reputation: 1045

Or, if you want to be really explicit, use

sys.stdout.write('\n')

Write method doesn't append line break by default. It's probably a bit more intuitive than an empty print.

Upvotes: 0

Remi Guan
Remi Guan

Reputation: 22292

From help(print) (I think you're using Python 3):

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

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:

file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

So print()'s default end argument is \n. That means you don't need add a \n like print('\n'). This will print two newlines, just use print().

By the way, if you're using Python 2, use print.

Upvotes: 1

dimo414
dimo414

Reputation: 48864

print()

will print a single new line in Python 3 (no parens needed in Python 2).

The docs for print() describe this behavior (notice the end parameter), and this question discusses disabling it.

Upvotes: 2

labheshr
labheshr

Reputation: 3056

print has a \n embedded in it....so you don't need to add \n by yourself

Upvotes: 0

Yu Hao
Yu Hao

Reputation: 122453

Because print automatically adds a new line, you don't have to do that manually, just call it with an empty string:

print("")

Upvotes: 1

Related Questions