当字符串中有多个“空”需要填写的时候,可以调用字符串的.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
本文介绍了Python中使用字符串的format()函数进行批量填充的方法,并通过示例展示了如何利用该函数简化字符串处理过程。此外,还提供了从用户输入获取数据并将其整合到URL中的实例。
9544

被折叠的 条评论
为什么被折叠?



