Lesson 06
print() with format() and Format Specifiers
Format console output with str.format() and with %d, %f, and %s specifiers.
What you will learn
By the end of this lesson, you'll understand:
- What is Syntax-4:
print()withformat()? - What is Syntax-5:
print()with format specifiers? - What are
%d,%f, and%s? - Why do lists and dictionaries require
%s(orstr())?
Why do we need formatting?
In the previous lesson, we displayed values using commas (addex2.py).
a = 10
print("Value =", a)Sometimes we need better control over how the output looks. Python provides two classic formatting methods from your notes:
str.format()%format specifiers
addex3.py already uses format() for the sum line.
Syntax-4: print() with format()
The format() method replaces {} placeholders with values.
print("Message {}".format(value)){} is a placeholder. Python fills them from left to right.
Example 1 — one value (FormatEx1.py)
# FormatEx1.py
a = 10
print("Val of a={}".format(a))
print("{} is the val of a".format(a))Val of a=10
10 is the val of aExample 2 — multiple values (FormatEx2.py)
# FormatEx2.py
a = 10
b = 20
c = a + b
print("sum of {} and {}={}".format(a, b, c))
print("sum({},{})={}".format(a, b, c))sum of 10 and 20=30
sum(10,20)=30Same pattern as the last line of addex3.py:
print("sum({},{})={}".format(a, b, c))Rule: placeholders are filled in order.
Example 3 — three numbers (FormatEx3.py)
# FormatEx3.py
a = 10
b = 20
c = 30
d = a + b + c
print("sum of {},{} and {}={}".format(a, b, c, d))sum of 10,20 and 30=60Syntax-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)
# SpecIntEx.py
a = 10
print("Val of a=%d" % a)Val of a=10%f — float (SpecFloatEx.py)
# SpecFloatEx.py
a = 2.3
print("Val of a=%f" % a)Val of a=2.300000Python displays 6 decimal places by default.
Controlling decimal places
a = 2.3
print("Val of a=%0.2f" % a)
print("Val of a=%0.3f" % a)
print("Val of a=%0.15f" % a)Val of a=2.30
Val of a=2.300
Val of a=2.300000000000000Rule: %0.2f means display 2 digits after the decimal.
%s — string (SpecStrEx.py)
# SpecStrEx.py
name = "Nikesh"
print("Name is %s" % name)Name is NikeshMixing types (StudentMarksEx.py)
# 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))My Number is 10 and Name is Nikesh and Marks is 34.56The tuple (sno, sname, marks) fills each specifier from left to right.
Formatting numbers together (SpecSumEx.py)
# 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))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.50Lists and dictionaries
Using format() (ListFormatEx.py)
# ListFormatEx.py
lst = [10, "RS", 23.45]
print("Content of lst={}".format(lst))
print("Content of lst=", lst)Content of lst=[10, 'RS', 23.45]
Content of lst= [10, 'RS', 23.45]Both methods work.
Why %d fails (ListSpecFailEx.py)
# ListSpecFailEx.py
lst = [10, "RS", 23.45]
print("Content of lst=%d" % lst)TypeError: %d format: a real number is required, not listA list is not an integer, so %d cannot format it.
Correct way — %s (ListSpecOkEx.py, DictSpecEx.py)
# ListSpecOkEx.py
lst = [10, "RS", 23.45]
print("Content of lst=%s" % str(lst))Content of lst=[10, 'RS', 23.45]# DictSpecEx.py
d1 = {10: "Python", 20: "Java", 30: "C"}
print("Content of d1={}".format(d1))
print("Content of d1=%s" % str(d1))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)
# 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)Employee ID : 101
Employee Name : Mahesh
Salary : 85675.50Key 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)%dformats integers;%fformats floats;%sformats strings%0.2fdisplays 2 digits after the decimal- Lists and dictionaries should use
%swithstr(), not%d
Quick knowledge check
-
Which symbol is used as a placeholder in
format()?
{} -
What does
%0.2fmean?
Display a float with 2 decimal places. -
Which specifier is used for strings?
%s -
Why does
%dfail for a list?
%dexpects an integer, but a list is not an integer.
Next lesson: using end= to print output on the same line.