Python Output
Output in Python means displaying information to the user.
The most commonly used function for output in Python is the
print() function.The
print() function is simple, powerful, and flexible.
It can display text, numbers, variables, and even the result of expressions.What is Output in Python?
Output allows a program to:
- Show results to the user
- Display messages
- Debug code
- Print data for verification
Python sends output to the standard output device, which is usually the screen.
Definition
Python output is the data that a program displays after execution, commonly using the print() function.
The print() Function
The
print() function is used to display output on the screen.
It can take one or multiple arguments and prints them in a readable format.Basic Syntax
python
print(value)
Code Example 1: Printing Text
python
print("Hello, Python")
Code Example 2: Printing a Number
python
print(100)
Code Example 3: Printing Expression Result
python
print(10 + 20)
Printing Variables
You can use
print() to display the value of variables.Code Example 1: Print a Variable
python
name = "Python"
print(name)
Code Example 2: Print Multiple Variables
python
x = 10
y = 20
print(x, y)
Code Example 3: Print Text and Variable Together
python
age = 25
print("Age is", age)
Using Separator and End Parameters
The
print() function provides optional parameters to control formatting.sep Parameter
Controls how multiple values are separated.
python
print("Python", "Java", "C++", sep=", ")
end Parameter
Controls what happens at the end of the output.
python
print("Hello", end=" ")
print("World")
Double Quotes in Python Print
Python allows strings to be written using double quotes.
Double quotes are commonly used when the text contains single quotes.
Code Example 1: Simple Double Quotes
python
print("Welcome to Python")
Code Example 2: Using Single Quote Inside Double Quotes
python
print("Python is called a 'high-level' language")
Code Example 3: Printing a Sentence
python
message = "Learning Python is easy"
print(message)
Best Practice
Double quotes are often preferred for clean and consistent code.
Print Without a New Line
By default,
print() adds a new line after printing output.
You can change this behavior using the end parameter.Code Example 1: Default New Line
python
print("Hello")
print("World")
Code Example 2: Print Without New Line
python
print("Hello", end="")
print(" World")
Code Example 3: Custom End Character
python
print("Loading", end="...")
Useful Tip
Printing without a new line is helpful for progress messages and formatted output.
Printing Multiple Lines
Python can print multiple lines using escape characters.
Code Example 1: New Line Character
python
print("Hello\nWorld")
Code Example 2: Multi-line Text
python
print("""This is line one
This is line two
This is line three""")
Why Python Output is Easy
Python output is:
- Simple to use
- Highly readable
- Flexible for formatting
- Beginner-friendly
Key Takeaway
The print() function is the primary way to communicate results from a Python program.
Python Print Numbers and Text
The
print() function in Python can be used to display numbers, text, and combinations of both.
Python automatically handles different data types, making output simple and readable.Print Numbers
In Python, numbers can be printed directly without using quotes.
Python supports integers, floating-point numbers, and complex numbers.
Important
Numbers should not be enclosed in quotes, otherwise they will be treated as text.
Code Example 1: Print an Integer
python
print(10)
Code Example 2: Print a Floating-Point Number
python
print(3.14)
Code Example 3: Print Result of a Calculation
python
print(25 + 75)
Print Numbers Stored in Variables
You can also print numeric values stored in variables.
Code Example 1: Print Variable Value
python
x = 100
print(x)
Code Example 2: Print Multiple Numbers
python
a = 5
b = 10
print(a, b)
Code Example 3: Print Calculated Value
python
x = 20
y = 30
print(x * y)
Mix Text and Numbers
Python allows mixing text and numbers in output.
This is commonly done using commas inside the
print() function.Key Concept
When using commas, Python automatically adds a space between values.
Code Example 1: Text and Number Together
python
age = 25
print("Age is", age)
Code Example 2: Multiple Text and Numbers
python
marks = 90
print("Marks obtained:", marks)
Code Example 3: Printing Calculation with Text
python
x = 10
y = 20
print("Sum is", x + y)
Using f-strings (Modern Way)
Python also provides f-strings to mix text and numbers in a clean way.
Best Practice
f-strings are the recommended way in modern Python.
Code Example 1: Basic f-string
python
age = 30
print(f"Age is {age}")
Code Example 2: Expression Inside f-string
python
a = 5
b = 7
print(f"Sum is {a + b}")
Code Example 3: Multiple Values
python
name = "Python"
version = 3
print(f"{name} version is {version}")
Common Mistake to Avoid
Trying to mix text and numbers using
+ causes an error.Incorrect Example
python
age = 20
print("Age is " + age)
Correct Example
python
age = 20
print("Age is", age)
Summary
- Numbers can be printed directly
- Quotes turn numbers into text
- Text and numbers can be mixed using commas
- f-strings provide clean formatting
- Avoid using
+with text and numbers
Exercise
- Print any two numbers
- Print text with a number using commas
- Print a calculation result using an f-string