当字符串中有多个“空”需要填写的时候,可以调用字符串的.format()函数进行批处理。基本使用方法如下代码所示:
> 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'))
>
> city = input('write down the name of city:') url =
> "http://apistore.baidu.com/microservice/weather?citypinyin={}".format(city)
> print(url)`
运行结果是
With a word she can get what she came for.
with a word she can get what she came for.
With a word she can get what she came for.
write down the name of city:beijing
http://apistore.baidu.com/microservice/weather?citypinyin=beijing
Process finished with exit code 0