Printing Without New Line in Python

In Python, the print() function ends with a new line by default. If we use print(variable) it goes to the next line automatically.

Below is an example:

print(“pstanalytics”)

print(“analytics”)

Output:

pstanalytics

analytics

 

But what if we don’t want to go to a new line and start from the same line.

Below is an example:

Input : print(“pstanalytics”) print(“analytics”)

Output :pstanalytics  analytics

 

Input : a = [1, 2, 3, 4]

Output : 1 2 3 4

The above example can vary with the version of Python.

 

Printing without new line: Python 2.x

# Python 2 code for printing

# on the same line printing

# pstanalytics and analytics

# in the same line

 

print(“pstanalytics”),

print(“analytics”)

 

# array

a = [1, 2, 3, 4]

 

# printing a element in same

# line

fori in range(4):

            print(a[i]),

Output:

pstanalytics   analytics

12  3  4

Printing without new line: Python 3.x

# Python 3 code for printing

# on the same line printing

# pstanalytics and analytics

# in the same line

 

print(“pstanalytics”, end =” “)

print(“analytics”)

 

# array

a = [1, 2, 3, 4]

 

# printing a element in same

# line

fori in range(4):

               print(a[i], end =” “)

Output:

pstanalytics  analytics

1  2  3  4

Leave a Reply

Your email address will not be published. Required fields are marked *