Remember
format(value, spec) produces formatted text. The format specification controls how the value is displayed.
format(value, spec) produces formatted text. The format specification controls how the value is displayed.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
number = 7
print(format(number, "04d"))Top-level scope
7format(value, spec) produces formatted text. The format specification controls how the value is displayed.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
number = 7
print(format(number, "04d"))
|
v
DISPLAYED RESULT
0007
FINAL BINDINGS
number --> object 1 (int)
object 1: 7Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
number = 7
print(format(number, "04d"))Top-level scope
7The example has finished. Compare the final output with the values and references above.
0007
What do you expect this program to print?
number = 7
print(format(number, "04d"))format() FunctionThe format() function in Python lets you format strings by inserting values into placeholders. It's a clean and powerful way to build strings with variables, numbers, and text.
string.format(value1, value2, ...)
value1, value2, ... – Values (strings, numbers, etc.) to insert into the stringname = "Alice"
age = 30
message = "My name is {} and I am {} years old.".format(name, age)
print(message)
My name is Alice and I am 30 years old.
msg = "The {1} comes after the {0}.".format("first", "second")
print(msg)
The second comes after the first.
msg = "Hello, {name}. You have {messages} new messages."
print(msg.format(name="John", messages=5))
Hello, John. You have 5 new messages.
price = 49.5678
print("Price: {:.2f}".format(price))
Price: 49.57
{:.2f} means: round to 2 decimal places
print("{:<10} | {:^10} | {:>10}".format("Left", "Center", "Right"))
Left | Center | Right
Tip: Use < for left, ^ for center, and > for right alignment.
product = "Pen"
qty = 10
unit_price = 1.5
print("Item: {}, Qty: {}, Total: ${:.2f}".format(product, qty, qty * unit_price))
Item: Pen, Qty: 10, Total: $15.00
IndexError{:abc}String formatting is frequently asked in Python coding rounds. The format() function is a powerful alternative to string concatenation.
format() inserts values into a string using placeholders {}Write a program to print: “Hello, my name is NAME and I scored SCORE% in Python.” using the format() function.
name = "Rahul"
score = 93.5
print("Hello, my name is {} and I scored {}% in Python.".format(name, score))
Connect what you noticed in the example with the idea below.
format(value, spec) produces formatted text. The format specification controls how the value is displayed.
format() changes the original number.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.