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("geeks") print("geeksforgeeks") |
will result into this
geeks geeksforgeeks
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("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 2 code for printing # on the same line printing # geeks and geeksforgeeks # in the same line print("geeks"), print("geeksforgeeks") # array a = [1, 2, 3, 4] # printing a element in same # line for i in range(4): print(a[i]), |
Output:
geeks geeksforgeeks 1 2 3 4
Print without newline in Python 3.x
# Python 3 code for printing # on the same line printing # geeks and geeksforgeeks # in the same line print("geeks", end =" ") print("geeksforgeeks") # array a = [1, 2, 3, 4] # printing a element in same # line for i in range(4): print(a[i], end =" ") |
Output:
geeks geeksforgeeks 1 2 3 4
Recommended Posts:
- Python | Ways to split strings using newline delimiter
- Print with your own font using Python !!
- Python | sep parameter in print()
- Python | end parameter in print()
- Python | Print all sublists of a list
- Python program to print Emojis
- Python | Output using print() function
- Python | Print diagonals of 2D list
- Print first m multiples of n without using any loop in Python
- Print Colors in Python terminal
- Print lists in Python (4 Different Ways)
- Python | Print the initials of a name with last name in full
- Python | file parameter in print()
- Python Program to Print Numbers in an Interval
- Python | Remove and print every third from list until it becomes empty
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



