Python Output
- Python output refers to displaying information or results on the screen when a program runs. The most common way to show output in Python is by using the print() function. It allows programmers to display text messages, variable values, calculation results, and formatted data in the console.
Printing Text in Python
The print() function is used to display text, numbers, and results on the screen.
It is the most basic and commonly used function for showing output in Python.
Print a Simple Welcome Message
This code uses the print() function to display a basic text message on the screen, showing how Python outputs information.
print("Welcome to Python Programming!")
Multiple print() Statements
You can use the print() function as many times as you want.
Each print() statement automatically moves to a new line, making the output neatly organized.
Printing Multiple Lines Using print()
This example shows how each print() call displays text on a new line, making it easy to output multiple messages one after another.
print("Python is easy to learn.")
print("It is powerful too!")
print("Let's start coding!")
Using Single or Double Quotes in Python
In Python, text (called strings) must be written inside quotes.
Python allows you to use either single (') or double (") quotes, and both work the same way.
Printing Strings with Single and Double Quotes
This example shows that Python treats single (') and double (") quotes equally, allowing you to write strings using either style.
print("This is valid.")
print('This is also valid.')
Missing Quotes in a print() Statement
This example shows what happens when you try to print text without quotes. Python cannot read it as a string, so it raises a syntax error.
print(Hello Python)
- Printing Numbers in Python
The print() function can display numbers directly.
Numbers are written without quotes, because Python treats them as numerical values, not text.
Printing Numeric Values
This example shows how Python prints numbers without using quotes, since they are interpreted as numeric data rather than strings.
print(10)
print(250)
print(99999)
Printing Text on the Same Line
By default, every print() statement moves the cursor to a new line.
To make two print statements appear on the same line, you can use the end parameter, which controls what is printed at the end of the line.
Print Multiple Messages on the Same Line
This example uses the end=" " parameter to prevent a line break, allowing both print statements to appear together on one line.
print("Python is fun", end=" ")
print("to learn!")
end=" " replaces Python’s default newline with a space, allowing the next print statement to continue on the same line.
Printing Without Space Using end=""
This example shows how using end="" removes all spacing, causing the next printed text to join directly without any gap.
print("Hello", end="")
print("World")
Printing Numbers in Python
The print() function isn’t just for text—it can also display numbers.
In Python, numbers are written without quotes, because they are treated as actual numeric values, not strings.
Displaying Numeric Values Using print()
This example shows how to print numbers directly. Since they are numeric types, no quotation marks are needed.
print(10)
print(250)
print(99999)
Doing Calculations Inside print()
Python can perform mathematical operations directly inside the print() function.
You can add, subtract, multiply, divide, or combine any valid expressions.
Printing the Results of Mathematical Expressions
This example shows how Python evaluates arithmetic expressions inside print() and displays the computed results.
print(8 + 4)
print(6 * 5)
print(20 - 3)
print(15 / 3)
Combine Text and Numbers
If you want to display text along with numbers, you can separate them with commas inside the print() function.
print("My score is", 95)
print("The total price is", 1200, "rupees.")
Another Example (Age Calculator)
birth_year = 2000
current_year = 2025
print("Your age is", current_year - birth_year)