In this article let’s learn about how to Print Without a Newline in Python. The built-in Python print() function is used to print the specified text inside the command prompt. The newline character is added at the end by default when using Python’s print command.
However, there are situations where we don’t want to go to next line but want to print on the same line. So what can we do?
a = ['similar','geeks',3, 4] print(a)
Output : [‘similar’, ‘geeks’, 3, 4]
The solution depends totally on the python version you are using.
Using for loop
# Python 2 code for printing on the same line printing print("geeks"), print("similargeeks") # array s = [1, 2, 3, 4, 5] # printing array elements in the same line for i in range(4): print(s,[i]),
1 2 3 4 5
With Python 3 , the end= option has been added as a second parameter for print(). This parameter handles eliminating the newline that is added by default in print().
print("Hi!! ", end="") print("Welcome to Similar Geeks")
How to Get Python dictionary keys as a List
How to Save List to CSV in Python