Lesson 05
Display Results with print()
Use print() to show values, string messages, and mixed messages-plus-values on the console.
What you will learn
By the end of this lesson, you'll understand:
- Why do we use the pre-defined function
print()? - What is Syntax-1: printing values?
- What is Syntax-2: printing string messages?
- What is Syntax-3: printing messages cum values?
What is print()?
In the previous lesson you stored values in variables such as a, b, and c. Now you will display those values on screen.
print() is a pre-defined Python function used to display the result of a Python program on the console.
Program flow
Purpose: display values, messages, or both to the user.
Syntax-1: print values
Use this syntax to display values or variable values.
print(Val1)
print(Val1, Val2, ..., Val_n)Here, Val1, Val2, and so on represent numeric values or variables.
Example 1 — single value (PrintValEx1.py)
# PrintValEx1.py
a = 10
print(a)10print() displays the value stored inside a.
Example 2 — multiple values (addex1.py)
This is the uploaded Batch Mode file:
# addex1.py — adding of two numbers
a = 10
b = 20
c = a + b
print(a)
print(b)
print(c)10
20
30One print() with several values:
# PrintValEx2.py
a = 10
b = 20
c = a + b
print(a, b, c)
print(100, 200, 300)10 20 30
100 200 300Important rule
When multiple values are separated by a comma, Python inserts a space.
| Code | Output |
|---|---|
print(1, 2) |
1 2 |
print(10, 20, 30) |
10 20 30 |
print(a, b, c) |
10 20 30 |
Syntax-2: print string messages
Use this syntax to display messages (strings).
print(msg1)
print(msg1, msg2)
print(msg1 + msg2)Here msg1 and msg2 are string values.
Example 1 — single message (PrintMsgEx1.py)
# PrintMsgEx1.py
print("Hello Python Students")Hello Python StudentsExample 2 — multiple messages with comma (PrintMsgEx2.py)
# PrintMsgEx2.py
print("Hello", "Python", "Students")Hello Python StudentsPython inserts spaces because commas separate the strings.
Example 3 — concatenation with + (PrintConcatEx.py)
# PrintConcatEx.py
print("Hello" + "Python" + "Prog")HelloPythonProgThere are no spaces. Put spaces inside the string if you need them:
print("Hello" + " Python" + " Prog")Hello Python ProgComma vs plus (+)
This is one of the most important beginner ideas.
Comma (PrintCommaEx.py):
print("Hello", "Python")Hello PythonPlus (PrintPlusEx.py):
print("Hello" + "Python")HelloPythonSyntax-3: messages cum values
This syntax displays messages together with variable values.
Example 1 — addex2.py
This is the uploaded Batch Mode file:
# addex2.py — adding of two numbers
a = 10
b = 20
c = a + b
print("*" * 50)
print("Val of a=", a)
print("Val of b=", b)
print("sum=", c)
print("*" * 50)Shorter mix from the notes (PrintMixEx1.py):
# PrintMixEx1.py
a = 10
print("Val of a=", a)
print(a, "is the val of a")Val of a= 10
10 is the val of aThe comma lets you mix strings and variables in the same print().
Example 2 — sum of two numbers (PrintMixEx2.py)
# PrintMixEx2.py
a = 10
b = 20
c = a + b
print("sum=", c)
print(c, "is the sum")
print("sum of", a, "and", b, "=", c)sum= 30
30 is the sum
sum of 10 and 20 = 30Python combines the message and the values into one readable line.
Example 3 — sum of three numbers (PrintSum3Ex.py)
# PrintSum3Ex.py
a = 10
b = 20
c = 30
d = a + b + c
print("Sum of", a, ",", b, "and", c, "=", d)Sum of 10 , 20 and 30 = 60This follows the structure in your Display Result notes.
Three syntaxes at a glance
| Syntax | Purpose | Example file |
|---|---|---|
| Syntax-1 | Values | addex1.py, PrintValEx1.py |
| Syntax-2 | Messages | PrintMsgEx1.py, PrintConcatEx.py |
| Syntax-3 | Messages + values | addex2.py, PrintMixEx2.py |
Real-world example (StudentPrintEx.py)
# StudentPrintEx.py
name = "Mahesh"
age = 25
print("Student Name:", name)
print("Age:", age)
print(name, "is", age, "years old")Student Name: Mahesh
Age: 25
Mahesh is 25 years oldThis is the same messages cum values pattern used in real applications.
Key terms
| Term | Meaning |
|---|---|
print() |
Pre-defined function for console output |
| Console | The screen where output is displayed |
| String | Text enclosed in quotes |
| Variable | A named storage location for a value |
| Concatenation | Joining strings using + |
Summary
print()is the console output function in Python- Syntax-1 displays values or variables (
addex1.py) - Syntax-2 displays string messages (
PrintMsgEx1.py) - Syntax-3 displays messages together with values (
addex2.py) - Comma (
,) inserts spaces automatically - Plus (
+) concatenates strings without adding spaces
Quick knowledge check
-
Which pre-defined function displays output on the console?
print() -
What is the difference between comma and
+inprint()?
Comma adds spaces automatically;+joins strings without spaces. -
Which uploaded file shows messages cum values for the add program?
addex2.py -
What is the output of
print("A", "B")?
A B
Next lesson: format() and format specifiers.
Loading playground…