Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 05

Display Results with print()

Use print() to show values, string messages, and mixed messages-plus-values on the console.

Beginner15 min

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

Data flows through print() to the console

Purpose: display values, messages, or both to the user.

Syntax-1: print values

Use this syntax to display values or variable values.

python
print(Val1)
print(Val1, Val2, ..., Val_n)

Here, Val1, Val2, and so on represent numeric values or variables.

Example 1 — single value (PrintValEx1.py)

python
# PrintValEx1.py
a = 10
print(a)
text
10
a = 10 goes through print(a) to output 10

print() displays the value stored inside a.

Example 2 — multiple values (addex1.py)

This is the uploaded Batch Mode file:

python
# addex1.py — adding of two numbers
a = 10
b = 20
c = a + b
print(a)
print(b)
print(c)
text
10
20
30

One print() with several values:

python
# PrintValEx2.py
a = 10
b = 20
c = a + b
print(a, b, c)
print(100, 200, 300)
text
10 20 30
100 200 300

Important 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).

python
print(msg1)
print(msg1, msg2)
print(msg1 + msg2)

Here msg1 and msg2 are string values.

Example 1 — single message (PrintMsgEx1.py)

python
# PrintMsgEx1.py
print("Hello Python Students")
text
Hello Python Students

Example 2 — multiple messages with comma (PrintMsgEx2.py)

python
# PrintMsgEx2.py
print("Hello", "Python", "Students")
text
Hello Python Students

Python inserts spaces because commas separate the strings.

Example 3 — concatenation with + (PrintConcatEx.py)

python
# PrintConcatEx.py
print("Hello" + "Python" + "Prog")
text
HelloPythonProg

There are no spaces. Put spaces inside the string if you need them:

python
print("Hello" + " Python" + " Prog")
text
Hello Python Prog

Comma vs plus (+)

This is one of the most important beginner ideas.

Comma (PrintCommaEx.py):

python
print("Hello", "Python")
text
Hello Python

Plus (PrintPlusEx.py):

python
print("Hello" + "Python")
text
HelloPython
Comma inserts spaces; plus joins strings

Syntax-3: messages cum values

This syntax displays messages together with variable values.

Example 1 — addex2.py

This is the uploaded Batch Mode file:

python
# 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):

python
# PrintMixEx1.py
a = 10
print("Val of a=", a)
print(a, "is the val of a")
text
Val of a= 10
10 is the val of a

The comma lets you mix strings and variables in the same print().

Example 2 — sum of two numbers (PrintMixEx2.py)

python
# PrintMixEx2.py
a = 10
b = 20
c = a + b
print("sum=", c)
print(c, "is the sum")
print("sum of", a, "and", b, "=", c)
text
sum= 30
30 is the sum
sum of 10 and 20 = 30
print() builds: sum of + 10 + and + 20 = 30

Python combines the message and the values into one readable line.

Example 3 — sum of three numbers (PrintSum3Ex.py)

python
# PrintSum3Ex.py
a = 10
b = 20
c = 30
d = a + b + c
print("Sum of", a, ",", b, "and", c, "=", d)
text
Sum of 10 , 20 and 30 = 60

This 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)

python
# StudentPrintEx.py
name = "Mahesh"
age = 25
print("Student Name:", name)
print("Age:", age)
print(name, "is", age, "years old")
text
Student Name: Mahesh
Age: 25
Mahesh is 25 years old

This 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

  1. Which pre-defined function displays output on the console?
    print()

  2. What is the difference between comma and + in print()?
    Comma adds spaces automatically; + joins strings without spaces.

  3. Which uploaded file shows messages cum values for the add program?
    addex2.py

  4. What is the output of print("A", "B")?
    A B

Next lesson: format() and format specifiers.

Loading playground…