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

Lesson 06

print() with format() and Format Specifiers

Format console output with str.format() and with %d, %f, and %s specifiers.

Beginner20 min

What you will learn

By the end of this lesson, you'll understand:

  • What is Syntax-4: print() with format()?
  • What is Syntax-5: print() with format specifiers?
  • What are %d, %f, and %s?
  • Why do lists and dictionaries require %s (or str())?

Why do we need formatting?

In the previous lesson, we displayed values using commas (addex2.py).

python
a = 10
print("Value =", a)

Sometimes we need better control over how the output looks. Python provides two classic formatting methods from your notes:

  1. str.format()
  2. % format specifiers

addex3.py already uses format() for the sum line.

Syntax-4: print() with format()

The format() method replaces {} placeholders with values.

python
print("Message {}".format(value))
Placeholder {} filled by format(10)

{} is a placeholder. Python fills them from left to right.

Example 1 — one value (FormatEx1.py)

python
# FormatEx1.py
a = 10
print("Val of a={}".format(a))
print("{} is the val of a".format(a))
text
Val of a=10
10 is the val of a

Example 2 — multiple values (FormatEx2.py)

python
# FormatEx2.py
a = 10
b = 20
c = a + b
print("sum of {} and {}={}".format(a, b, c))
print("sum({},{})={}".format(a, b, c))
text
sum of 10 and 20=30
sum(10,20)=30

Same pattern as the last line of addex3.py:

python
print("sum({},{})={}".format(a, b, c))
Placeholders fill a, then b, then c

Rule: placeholders are filled in order.

Example 3 — three numbers (FormatEx3.py)

python
# FormatEx3.py
a = 10
b = 20
c = 30
d = a + b + c
print("sum of {},{} and {}={}".format(a, b, c, d))
text
sum of 10,20 and 30=60

Syntax-5: format specifiers (%)

This is the classic formatting style in Python.

Specifier Meaning Data type
%d Integer int
%f Floating-point float
%s String str

Think of them as placeholders for specific data types.

%d — integer (SpecIntEx.py)

python
# SpecIntEx.py
a = 10
print("Val of a=%d" % a)
text
Val of a=10
%d accepts integer 10

%f — float (SpecFloatEx.py)

python
# SpecFloatEx.py
a = 2.3
print("Val of a=%f" % a)
text
Val of a=2.300000

Python displays 6 decimal places by default.

Controlling decimal places

python
a = 2.3
print("Val of a=%0.2f" % a)
print("Val of a=%0.3f" % a)
print("Val of a=%0.15f" % a)
text
Val of a=2.30
Val of a=2.300
Val of a=2.300000000000000
%0.2f means 2 decimal digits

Rule: %0.2f means display 2 digits after the decimal.

%s — string (SpecStrEx.py)

python
# SpecStrEx.py
name = "Nikesh"
print("Name is %s" % name)
text
Name is Nikesh

Mixing types (StudentMarksEx.py)

python
# StudentMarksEx.py
sno = 10
sname = "Nikesh"
marks = 34.56
print("My Number is %d and Name is %s and Marks is %0.2f" % (sno, sname, marks))
text
My Number is 10 and Name is Nikesh and Marks is 34.56
%d, %s, %0.2f filled from a tuple

The tuple (sno, sname, marks) fills each specifier from left to right.

Formatting numbers together (SpecSumEx.py)

python
# SpecSumEx.py
a = 10
b = 2.5
c = a + b
print("sum(%d,%0.2f)=%0.2f" % (a, b, c))
print("sum(%f,%0.2f)=%0.2f" % (a, b, c))
print("sum(%0.2f,%0.2f)=%0.2f" % (a, b, c))
print("sum(%0.2f,%d)=%0.2f" % (a, b, c))
text
sum(10,2.50)=12.50
sum(10.000000,2.50)=12.50
sum(10.00,2.50)=12.50
sum(10.00,2)=12.50

Lists and dictionaries

Using format() (ListFormatEx.py)

python
# ListFormatEx.py
lst = [10, "RS", 23.45]
print("Content of lst={}".format(lst))
print("Content of lst=", lst)
text
Content of lst=[10, 'RS', 23.45]
Content of lst= [10, 'RS', 23.45]

Both methods work.

Why %d fails (ListSpecFailEx.py)

python
# ListSpecFailEx.py
lst = [10, "RS", 23.45]
print("Content of lst=%d" % lst)
text
TypeError: %d format: a real number is required, not list
%d is integer-only; a list causes TypeError

A list is not an integer, so %d cannot format it.

Correct way — %s (ListSpecOkEx.py, DictSpecEx.py)

python
# ListSpecOkEx.py
lst = [10, "RS", 23.45]
print("Content of lst=%s" % str(lst))
text
Content of lst=[10, 'RS', 23.45]
python
# DictSpecEx.py
d1 = {10: "Python", 20: "Java", 30: "C"}
print("Content of d1={}".format(d1))
print("Content of d1=%s" % str(d1))
text
Content of d1={10: 'Python', 20: 'Java', 30: 'C'}
Content of d1={10: 'Python', 20: 'Java', 30: 'C'}

format() vs % specifiers

Feature format() % specifiers
Placeholder {} %d, %f, %s
Integer {} %d
Float {} %f
String {} %s
Decimal precision Advanced options %0.2f
Collections Works directly Use %s + str()

Real-world example (EmployeeFormatEx.py)

python
# EmployeeFormatEx.py
emp_id = 101
name = "Mahesh"
salary = 85675.5
print("Employee ID : {}".format(emp_id))
print("Employee Name : {}".format(name))
print("Salary : %0.2f" % salary)
text
Employee ID : 101
Employee Name : Mahesh
Salary : 85675.50

Key terms

Term Meaning
format() Replaces {} placeholders with values
{} Placeholder
%d Integer format specifier
%f Float format specifier
%s String format specifier
%0.2f Float with 2 decimal places
str() Converts a value into string type

Summary

  • format() fills {} placeholders in order (FormatEx2.py, addex3.py)
  • %d formats integers; %f formats floats; %s formats strings
  • %0.2f displays 2 digits after the decimal
  • Lists and dictionaries should use %s with str(), not %d

Quick knowledge check

  1. Which symbol is used as a placeholder in format()?
    {}

  2. What does %0.2f mean?
    Display a float with 2 decimal places.

  3. Which specifier is used for strings?
    %s

  4. Why does %d fail for a list?
    %d expects an integer, but a list is not an integer.

Next lesson: using end= to print output on the same line.