How to Print Without Newline in Python
Sometimes we wants to print string output without newline. Means printing a variable value or statement without going to newline. In Python for printing values print() function is used, which by default ends with newline.
Run below program which prints two string values
print("CrazyGeeks.ORG is one of the best Website ") print("in the world.")
You will see following output:
in the world.
Now run below code you will see a different output:
print("CrazyGeeks.ORG is one of the best Website "), print("in the world.")
The output should be:
Solution
Method 1: If you see above second program carefully, after first print() function comma (,) is used for skipping newline. So to print without newline you can use comma after print function.
For example:
print("Google is the best "), print("search engine.")
The output should be:
Method 2: Alternatively you can also use second parameter in print() function which is end
where you can assign value like end=" "
For example:
print("CrazyGeeks.ORG is one of the best Website ", end =" ") print("in the world.")
The output should be:
Examples
Example 1: using comma
# Python 3 code for printing without newline print("Apple"), print("Pineapple") # array a = [1, 2, 3, 4, 5, 6, 7] # printing a element in same # line for i in range(7): print(a[i]),
The output should be:
1 2 3 4 5 6 7
Example 2: using end = " "
# Python 3 code for printing without newline print("Apple", end=" ") print("Pineapple") # array a = [1, 2, 3, 4, 5, 6, 7] # printing a element in same # line for i in range(7): print(a[i],end=" ")
The output should be:
1 2 3 4 5 6 7
LATEST POSTS
-
Bubble Sort in Java
-
numpy.mean() in Python
-
Python square root sqrt() method
-
numpy.linspace() in Python
-
Read and Write text to files in Python
-
numpy.where() in Python with Examples
-
numpy.transpose() in Python
-
C strcmp() function with example
-
C++ abs() Absolute Value with Examples
-
numpy.arange() in Python
-
numpy.loadtxt() in Python
-
C++ do-while loop with Example