Remember
format_map() fills named fields using a mapping. A field’s name is looked up as a key.
format_map() fills named fields using a mapping. A field’s name is looked up as a key.
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.
data = {"name": 'Ada'}
print("Hello, {name}!".format_map(data))Top-level scope
{'name': 'Ada'}format_map() fills named fields using a mapping. A field’s name is looked up as a key.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
data = {"name": 'Ada'}
print("Hello, {name}!".format_map(data))
|
v
DISPLAYED RESULT
Hello, Ada!
FINAL BINDINGS
data --> object 1 (dict)
object 1: {'name': '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.
data = {"name": 'Ada'}
print("Hello, {name}!".format_map(data))Top-level scope
{'name': '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?
data = {"name": 'Ada'}
print("Hello, {name}!".format_map(data))String format_map() MethodThe format_map() method in Python is used to format a string using a dictionary or any mapping object. It's similar to str.format(), but instead of passing variables as keyword arguments, you pass a dictionary.
string.format_map(mapping)
mapping – A dictionary or mapping object containing keys that match the placeholders in the string.person = {"name": "Alice", "age": 30}
text = "Name: {name}, Age: {age}".format_map(person)
print(text)
Name: Alice, Age: 30
class DefaultDict(dict):
def __missing__(self, key):
return 'N/A'
info = DefaultDict(name="Bob")
print("Name: {name}, City: {city}".format_map(info))
Name: Bob, City: N/A
This technique is useful when some keys might be missing in the dictionary.
format() uses keyword arguments: "{name}".format(name="John")format_map() uses a dictionary: "{name}".format_map({"name": "John"})format_map() cannot take multiple types of arguments like format()TypeError.KeyError unless handled using __missing__.Use format_map() when formatting strings from dynamic dictionaries. It's helpful when you can't hardcode keys into the function call.
format_map() formats a string using a dictionary.format() but takes a single mapping object.Create a program that takes user input (name and score) and formats the output using format_map().
data = {"name": input("Enter your name: "), "score": input("Enter your score: ")}
template = "Player: {name} | Score: {score}"
print(template.format_map(data))
Connect what you noticed in the example with the idea below.
format_map() fills named fields using a mapping. A field’s name is looked up as a key.
format_map() replaces fields based on their numeric position only.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.