The Print is a built-in function in Python that is used to display text on the console or output screen. The default Print function outputs the text with endline. In this tutorial, we will have a look at different versions of print statements.
Working of Normal print() function
The print function takes a string or variable as input. As shown in the example below. The normal print function adds an end line at the end of the line.
print(“Hello World”) print(“Simple print adds an end line.”) |
How to print without a newline in Python?
We just need to add an end parameter at the end of the print statement as shown in the image below.
print(“Hello World” , end=’ ‘) print(“Simple print adds an end line.”) |
We can also end our line with a dot(.)
print(“Hello World ” , end=’.’) print(“Simple print adds an end line.”) |
Using Python sys module
There is another way to print on the console using the sys library. When using the sys library function write it does not add end line.
import sys sys.stdout.write(“Hello World “) sys.stdout.write(“Welcome to h2kinfosys website”) |
Using print() to print a list without a newline
If we simple use for loop to print the elements of the list the following will be the output.
colors = [“Red”, “Green”, “Blue”, “Yellow”, “Brown”] for i in colors: print(i) |
For printing all the list items in a single line we simply need to add the end parameter.
colors = [“Red”, “Green”, “Blue”, “Yellow”, “Brown”] for i in colors: print(i, end=’ ‘) |
One Response