How to print without newline in Python?

Course Curriculum

How to print without newline in Python?

How to print without newline in Python?

Generally people switching from C/C++ to Python wonder how to print two or more variables or statements without going into a new line in python. Since the python print() function by default ends with newline. Python has a predefined format if you use print(a_variable) then it will go to next line automatically.
For examples:

print("prutor")
print("prutor")
will result into this

prutor
prutor
But sometime it may happen that we don’t want to go to next line but want to print on the same line. So what we can do?
For Example:

Input : print("prutor") print("prutor")
Output : prutor prutor

Input : a = [1, 2, 3, 4]
Output : 1 2 3 4
The solution discussed here is totally dependent on the python version you are using.

Print without newline in Python 2.x

# Python 2 code for printing
# on the same line printing
# prutor and prutor
# in the same line

print("prutor"),
print("prutor")

# array
a = [1, 2, 3, 4]

# printing a element in same
# line
for i in range(4):
print(a[i]),
Output:

prutor prutor
1 2 3 4
Print without newline in Python 3.x

# Python 3 code for printing
# on the same line printing
# prutor and prutor
# in the same line

print("prutor", end =" ")
print("prutor")

# array
a = [1, 2, 3, 4]

# printing a element in same
# line
for i in range(4):
print(a[i], end =" ")
Output:

prutor prutor
1 2 3 4

How to assign values to variables in Python and other languages (Prev Lesson)
(Next Lesson) Python if else