%s: 格式化输出文字或数字
%d: 格式化输出数字
format: 格式化输出数字或文字
- # 格式化输出:
- # 1,%s,%d 两种当输出的字符串中没有 % 时优先 (%s,%d)
- # 2,format() 格式化输出 当字符串出现多个 % 时优先用 format()
- # format() 语法:"xx{}x".format(参数)
- # url = "https://search.51job.com/list/040000,000000,0000,00,9,99," \
- # "%E4%BA%BA%E4%BA%8B%E4%B8%BB%E7%AE%A1,2,{}" \
- # ".html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99" \
- # "&providesalary=99&lonlat=0,0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=".format(i) # 应该用 format
- # url1 = "https://movie.douban.com/top250?start=%s&filter=" % (i*25) # 用 %s
- >>>"{} {}".format("hello", "world") # 不设置指定位置, 按默认顺序
- 'hello world'
- >>> "{0} {1}".format("hello", "world") # 设置指定位置
- 'hello world'
- >>> "{1} {0} {1}".format("hello", "world") # 设置指定位置'world hello world'
- print("网站名:{name}, 地址 {url}".format(name="菜鸟教程", url="www.runoob.com"))
- # 通过字典设置参数
- site = {
- "name": "菜鸟教程", "url": "www.runoob.com"
- }
- print("网站名:{name}, 地址 {url}".format(**site))
- # 通过列表索引设置参数
- my_list = ['菜鸟教程', 'www.runoob.com']
- print("网站名:{0[0]}, 地址 {0[1]}".format(my_list)) # "0" 是必须的
输出结果:
网站名: 菜鸟教程, 地址 www.runoob.com
网站名: 菜鸟教程, 地址 www.runoob.com
网站名: 菜鸟教程, 地址 www.runoob.com
来源: http://www.bubuko.com/infodetail-2854268.html