Remember
str.format() replaces fields in a format string. The result is a new string.
str.format() replaces fields in a format string. The result is a new string.
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.
name = 'Ada'
message = "Hello, {}!".format(name)
print(message)Top-level scope
'Ada'Object 2 · str'Hello, Ada!'str.format() replaces fields in a format string. The result is a new string.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
name = 'Ada'
message = "Hello, {}!".format(name)
print(message)
|
v
DISPLAYED RESULT
Hello, Ada!
FINAL BINDINGS
name --> object 1 (str)
message --> object 2 (str)
object 1: 'Ada'
object 2: 'Hello, Ada!'Choose 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.
name = 'Ada'
message = "Hello, {}!".format(name)
print(message)Top-level scope
'Ada'Object 2 · str'Hello, Ada!'The example has finished. Compare the final output with the values and references above.
Hello, Ada!
What do you expect this program to print?
name = 'Ada'
message = "Hello, {}!".format(name)
print(message)str.format() MethodThe str.format() method in Python lets you insert values into a string using curly braces {} as placeholders. It’s a powerful and flexible way to build dynamic strings.
"string with {} placeholders".format(values...)
values... – One or more values to be inserted into the string in the order of the placeholders.name = "Alice"
message = "Hello, {}!".format(name)
print(message)
Hello, Alice!
name = "Bob"
age = 30
intro = "My name is {} and I am {} years old.".format(name, age)
print(intro)
My name is Bob and I am 30 years old.
sentence = "The {1} is {0} years old.".format(10, "car")
print(sentence)
The car is 10 years old.
Why? The numbers inside {} refer to the index of values passed into format().
info = "Name: {name}, Age: {age}".format(name="Charlie", age=25)
print(info)
Name: Charlie, Age: 25
price = 49.99
text = "Price: ${:.2f}".format(price)
print(text)
Price: $49.99
Why? :.2f formats the float to 2 decimal places.
In coding interviews, use str.format() when building readable strings, especially in questions involving string construction or reporting.
str.format() lets you insert variables into strings using {}Write a program that takes a name and score from the user and prints a sentence using str.format():
name = input("Enter your name: ")
score = input("Enter your score: ")
print("Hello, {}. You scored {} points.".format(name, score))
Connect what you noticed in the example with the idea below.
str.format() replaces fields in a format string. The result is a new string.
str.format() changes the format string in place.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.