Python print() Function Guide

What is print()?

The print() function in Python is used to display output on the screen. It's commonly used for printing text, variables, numbers, or results of expressions.

Basic Syntax

print(value1, value2, ..., sep=' ', end='\n')

Examples

1. Print a String

print("Hello, world!")

2. Print a Number

print(42)

3. Print Multiple Values


name = "Alice"
age = 25
print("Name:", name, "Age:", age)
        

4. Custom Separator

print("apple", "banana", "cherry", sep=", ")

5. Custom End


print("Hello", end=" ")
print("World")
        

6. Print Expressions


a = 5
b = 3
print("Sum:", a + b)
        

7. f-Strings (Modern Python)


name = "Bob"
score = 90
print(f"{name} scored {score} points.")
        

Summary

The print() function is essential for debugging and displaying output in Python. It supports formatting and customization through sep, end, and f-strings.