格式化输出 %s、%d以及format

%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,"
# "%25E4%25BA%25BA%25E4%25BA%258B%25E4%25B8%25BB%25E7%25AE%25A1,2,{}"
# ".html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99"
# "&providesalary=99&lonlat=0%2C0&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

格式化输出 %s、%d以及format

格式化输出 %s、%d以及format

格式化输出 %s、%d以及format