In this lesson, you’ll get a review of the two old school string formatting methods:
%
string formattingstr.format()
method
Here is an example of %
string formatting:
>>> name = "Eric"
>>> age = 74
>>> "Hello, %s. You are %s." % (name, age)
'Hello Eric. You are 74.'
The lesson also covers the str.format
method, which is an improvement on %
formatting.
Here’s the code for the examples used:
>>> "Hello, {}. You are {}.".format(name, age)
'Hello, Eric. You are 74.'
And:
>>> person = {'name': 'Eric', 'age': 74}
>>> "Hello, {name}. You are {age}.".format(name=person['name'], age=person['age'])
'Hello, Eric. You are 74.'
Terry Spotts on March 14, 2019
What about
.format_map(dictionary)
? I think it has some performance advantages over.format(**dictionary)
?