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 a newline. Python has a
predefined format if you use print(a_variable) then it will go to the next line
automatically.
For example:
● Python3
print("geeks")
print("geeksforgeeks
")
Will result in this:
geeks
geeksforgeeks
, But sometimes it may happen that we don’t want to go to the next line but
want to print on the same line. So what we can do?
For Example:
Input : print("geeks") print("geeksforgeeks")
Output : geeks geeksforgeeks
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
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 a newline. Python has a
predefined format if you use print(a_variable) then it will go to the next line
automatically.
For example:
● Python3
print("geeks")
print("geeksforgeeks
")
Will result in this:
geeks
geeksforgeeks
, But sometimes it may happen that we don’t want to go to the next line but
want to print on the same line. So what we can do?
For Example:
Input : print("geeks") print("geeksforgeeks")
Output : geeks geeksforgeeks
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