Python - Preventing Line Breaks with the print Function

If you don’t want to output a line break with the print function, specify an empty string for the end argument.

How to Prevent Line Breaks with the print Function

print(<string>, end='')
print(<string>, end="")

Explanation

The print function outputs the given string concatenated with the value of the end argument. The default value of the end argument is the newline character ‘\n’, so specifying an empty string prevents a line break.

Note: If you specify None, it is considered as if the argument was omitted, and a line break will be output.

Sample Code

The following sample code displays the string ‘Hello, World!’ with and without line breaks.
print('With line break')
print('Hello, World!')
print('Hello, World!')

print('Without line break', end='')
print('Hello, World!', end='')
print('Hello, World!', end='')
Execution result of the sample code(without line break)
Execution result of the sample code(without line break)

Application: Outputting Empty Lines

If you specify two line breaks for the end argument, an empty line will be output.
print('Output empty line', end='\n\n')
print('Hello, World!', end='\n\n')
print('Hello, World!', end='\n\n')
Execution result of the sample code(outputting empty lines)
Execution result of the sample code(outputting empty lines)

References

Test Environment