Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to print to the Screen using Python?
The print() function is the standard way to display output to the screen in Python. It can handle various data types and offers flexible formatting options.
Basic Print Statement
In Python 3, the print function requires parentheses −
print('Hello, world')
Hello, world
Printing Multiple Items
To print multiple items on the same line separated by spaces, use commas between them −
print('Hello,', 'World')
Hello, World
Printing Different Data Types
The print function can handle arbitrary data types in the same statement −
import os print(1, 0xff, 0o777, (1+5j), -0.999, 'text', [1, 2, 3])
1 255 511 (1+5j) -0.999 text [1, 2, 3]
Controlling Line Endings
Use the end parameter to control what gets printed at the end of each print statement −
for i in range(5):
print(i, end=' ')
print() # Empty print for newline
print('Next line')
0 1 2 3 4 Next line
Custom Separators
Use the sep parameter to change how multiple items are separated −
print('apple', 'banana', 'cherry', sep=' | ')
print(1, 2, 3, 4, sep='-')
apple | banana | cherry 1-2-3-4
Print Parameters Summary
| Parameter | Purpose | Default Value |
|---|---|---|
sep |
Separator between items | Single space |
end |
What to print at the end | Newline (\n) |
Conclusion
Python's print() function is versatile for displaying output. Use sep to control item separation and end to control line endings for precise output formatting.
Advertisements
