In this lesson, you’ll get a review of the two old school string formatting methods:
%
string formattingstr.format()
methodHere is an example of %
string formatting:
>>> name = "Eric"
>>> age = 74
>>> "Hello, %s. You are %s." % (name, age)
'Hello Eric. You are 74.'
Copied!
You’ll see how using this approach can lead to problems when writing longer strings.
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.'
Copied!
And:
>>> person = {'name': 'Eric', 'age': 74}
>>> "Hello, {name}. You are {age}.".format(name=person['name'], age=person['age'])
'Hello, Eric. You are 74.'
Copied!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4