Python string formatters

Python String Formatting Characters

____a word she can get what she ____ for.

A.With B.came

Fill-in-the-blank questions like these are particularly impressive. When a string contains multiple “blanks” like this, we can use .format() for batch processing. Its basic usage is as follows: Enter the code:

print('{} a word she can get what she {} for.'.format('With','came'))
print('{preposition} a word she can get what she {verb} for'.format(preposition = 'With',verb = 'came'))
print('{0} a word she can get what she {1} for.'.format('With','came'))

This string filling method is widely used. For example, the following code can fill in the missing city data in a URL:

city = input("write down the name of the city:")
url = "http://apistore.baidu.com/microservice/weather?citypinyin={}".format(city)

Note: This is a code snippet for developing a client-side weather plugin using the weather API provided by Baidu.

Okay, now you have mastered the basic concepts and common methods of variables and strings.

Next, we’ll continue to learn more about loops and functions.

Leave a Reply

Your email address will not be published. Required fields are marked *