concept placeholder in category python

appears as: placeholders, placeholder, placeholders, placeholder
Tiny Python Projects: Learn coding and testing with puzzles and games

This is an excerpt from Manning's book Tiny Python Projects: Learn coding and testing with puzzles and games.

To do so, you create a string template with curly brackets {}, which indicate placeholders for values. The values that will be substituted are arguments to the str.format() method, and they are substituted in the same order that the {} appear (figure 2.4).

In this chapter, we’ll write a program called mad.py that will read a file given as a positional argument and find all the placeholders in angle brackets, like <verb> or <adjective>. For each placeholder, we’ll prompt the user for the part of speech being requested, like “Give me a verb” and “Give me an adjective.” (Notice that you’ll need to use the correct article, just as in chapter 2.) Each value from the user will then replace the placeholder in the text, so if the user says “drive” for the verb, then <verb> in the text will be replaced with drive. When all the placeholders have been replaced with inputs from the user, we’ll print out the new text.

>>> for placeholder, name in matches:
...     print(f'Give me {name}')
...
Give me adjective
Give me noun
Give me preposition
Give me noun

If, however, we have values for the inputs, we can use those and not bother with the input() function. I’m only making you handle the --inputs option for testing purposes. You can safely assume that you will always have the same number of inputs as you have placeholders (see figure 17.11).

Figure 17.11 If given inputs from the command line, they will match up with the placeholders in the text.
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest