Computer >> Computer tutorials >  >> Programming >> Python

How to print in same line in Python?


The print() method in Python automatically prints in the next line each time. The print() method by default takes the pointer to the next line.

Example

for i in range(5):
   print(i)

Output

0
1
2
3
4

Modify print() method to print on the same line

The print method takes an extra parameter end=” “ to keep the pointer on the same line.

The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.

Syntax

print(“…..” , end=” “)

Print on same line with space between each element

The end=” “ is used to print in the same line with space after each element. It prints a space after each element in the same line.

Example

for i in range(5):
   print(i,end=" ")

Output

0 1 2 3 4

Print on same line without space between elements

The end=”” is used to print on same line without space. Keeping the doube quotes empty merge all the elements together in the same line.

Example

for i in range(5):
   print(i,end="")

Output

01234

Print on same line with some sign between elements

The end=”,” is used to print in the same line with a comma after each element. We can use some other sign such as ‘.’ or ‘;’ inside the end parameter.

Example

for i in range(5):
   print(i,end=",")
   print(i,end=".")

Output

0,1,2,3,4,
0.1.2.3.4.