In Python, the print()
function is used to display text or numbers on the screen. It’s like telling the computer to speak out loud. The print()
function is one of the most commonly used functions in Python, particularly for debugging and providing user feedback.
Basic usage
The most straightforward usage of the print()
function involves passing a string or number to it, which it then displays on the screen. For instance, consider the following example:
When this line of code is executed, it tells the computer to display the words "Hello, World!" on the screen. This simple command is often the first program written by beginners learning Python and serves as a gentle introduction to how Python handles output.
Printing multiple items
The print()
function can also handle multiple items separated by commas. When multiple items are passed to print()
, they are displayed with a space between each item. For example:
This line of code will output:
Quiz Question
The print statement is a versatile function that can handle various data types. Try matching these examples:
print("5" + "5")
print(123)
print(3+4)
print("2 + 5")
55
123
7
2 + 5
Hint: Consider how Python interprets different types of data when passed to the print function. What happens when you pass strings, integers, and expressions containing mathematical operations to print()
?
print("5" + "5") → 55
print(123) → 123
print(3+4) → 7
print("2 + 5") → 2 + 5