By default, Python
print() method adds a new line character (
\n) at the end of printed output on the screen. That is why all
print() statements display the output in a new line on the screen.
To print the objects in the same line in Python, we can follow given approaches:
1. Print Without New Line in Python 3
Use the end keyword argument in the
print() statment. By default, the value of
'\n' new line character. We have to change the default new line character with another character, e.g. a space character.
Example 1: Print strings in the Same Line with the space between two objects
print("A", end = ' ') print("B", end = ' ') print("C", end = ' ') print("D", end = '\n')
A B C D
Example 2: Printing without the space between two objects
The space character is optional. If we do not want to add any separator between the objects then use the empty string.
print("A", end = '') print("B", end = '') print("C", end = '') print("B", end = '\n')
2. Print Without New Line in Python 2
In Python 2, printing objects in same line is rather easy. We just need to add a comma (
',') character between the two
print "A", print "B", print "C", print "D"
A B C D
Happy Learning !!
Leave a Reply