zoukankan      html  css  js  c++  java
  • format字符串格式化【Python】

    # tpl='i am {},age{},{}'.format('火虎',32,'alex')
    # print(tpl)
    # 输出:i am 火虎,age32,alex 不一一对应则报错

    # tpl='i am {1},age{2},{0}'.format('火虎',32,'alex')
    # print(tpl)
    # 输出:i am 32,agealex,火虎 看看明白

    # tpl='i am {2},age{2},'.format('火虎',32,'alex')
    # print(tpl)
    # 输出:i am alex,agealex.

    # tpl='i am {2},age{2},'.format('火虎')
    # print(tpl)
    # 报错原因是前面2对应后面没有值,那么将2换做0即可

    # 字典的形式
    # tpl='i am {name},age{age},really{name}'.format(name='火虎',age=32,)
    # tpl='i am {name},age{age},really{name}'.format(**{'name':'火虎','age':32})
    #
    #
    # a={'name':'火虎','age':32}
    # tpl='i am {name},age{age},really{name}'.format(**a) #字典 两种方式
    #
    # print(tpl)
    # 输出:i am 火虎,age32,really火虎



    # ==============================================================================

    # tpl='i am {1[0]},age{0[2]},really{1[1]}'.format([1,2,3],[4,5,6])

    # print(tpl)
    # 输出:i am 4,age3,really5

    # tpl='我叫{:s},今年{:d},考试得分{:f}'.format('火虎',18,98.23)
    # print(tpl)
    # 输出:我叫火虎,今年18,考试得分98.230000

    # l=['火虎',18,98.123]
    # tpl='我叫{:s},今年{:d},考试得分{:f}'.format(*l)
    # print(tpl)
    # 同上

    #=============================================================================================
    tpl='numbers{:b},{:0},{:d},{:x},{:X},{:%}'.format(15,15,15,15,15,15.87)#b:转换成二进制,0:转换成八进制,d:转换成十进制,
    # x转换成十六进制(小写)X转换成十六进制(大写)%:就是百分号默认小数点后6位
    print(tpl)
    #输出:numbers1111,15,15,f,F,1587.000000%
  • 相关阅读:
    eclipse下配置hadoop环境
    eclipse下安装hadoop伪分布式集群
    ubuntu安装eclipse步骤
    Fiddler修改抓包请求
    Fiddler抓包(以谷歌浏览器、安卓手机为例)
    whistle抓包-数据包分析
    whistle手机抓包(以安卓手机为例)
    whistle浏览器抓包(以火狐浏览器为例)
    whistle安装
    Jmeter结构体系及运行原理
  • 原文地址:https://www.cnblogs.com/huohu66888/p/9783404.html
Copyright © 2011-2022 走看看