zoukankan      html  css  js  c++  java
  • 格式化输出 %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




  • 相关阅读:
    Nginx 限流配置
    Nginx 跨域配置
    LVS实现负载均衡原理及安装配置详解
    Tomcat基本概念
    Hapoxy 基本配置概念
    rsync断点续传
    Nginx概念
    angular img标签使用err-src
    $ionicLoading自定义加载动画
    h5+jquery自制相机,获取图片并上传
  • 原文地址:https://www.cnblogs.com/z520h123/p/9989240.html
Copyright © 2011-2022 走看看