Python Output

Python Output: The print() Function

In Python, the primary way to output data to the console or screen is by using the built-in print() function. It's one of the first functions any Python programmer learns.


1. Basic Printing

You can pass a string, number, or any other object to the print() function to display it.

Basic Print Example

print("Hello, World!")
print(123)

2. Printing Multiple Items

You can pass multiple items to the print() function, separated by commas. By default, it will separate each item with a single space.

Printing Multiple Items

x = "John"
y = 25
print("My name is", x, "and I am", y, "years old.")

3. Advanced Printing with sep and end

The print() function has optional parameters that give you more control over the output.

Using `sep` and `end`

// Using a custom separator
print("apple", "banana", "cherry", sep="---")

// Printing without a newline at the end print("Hello", end=" ") print("World!")


4. Formatted Strings (f-Strings)

Introduced in Python 3.6, f-Strings provide a concise and convenient way to embed expressions inside string literals for formatting.

f-String Example

name = "Jane"
age = 30
print(f"Her name is {name} and she is {age} years old.")

Exercise

?

Which parameter of the `print()` function do you use to specify the character that separates multiple items?