zoukankan      html  css  js  c++  java
  • format

    ---恢复内容开始---

    tp1='i am {},age{},{}'.format("seven",18,'alex')#不一一对应会报错
    print(tp1)

    输出

    i am seven,age18,alex
    tp2='i am {},age{},{}'.format("seven",18)
    print(tp2)

    输出报错

    IndexError: tuple index out of range
    tp2='i am {2},age{1},{0}'.format("seven",18,'alex')
    print(tp2)

    输出

    i am alex,age18,seven
    tp2='i am {1},age{1}'.format("seven",18,'alex')
    print(tp2)

    输出

    i am 18,age18
    tp2='i am {name},age{age},really{name}'.format(name="seven",age=18)
    print(tp2)

    输出

    i am seven,age18,reallyseven
    tp2='i am {name},age{age},really{name}'.format(**{'name':"seven",'age':18})
    print(tp2)

    输出

    i am seven,age18,reallyseven
    tp1='i am {0[0]},age{0[1]},really{0[2]}'.format([1,2,3],[11,22,33])
    print(tp1)

    输出

    i am 1,age2,really3
    tp1='i am {:s},age{:d},really{:f}'.format("seven",18,88.7)
    print(tp1)

    输出

    i am seven,age18,really88.700000
    tp1='i am {name:s},age{age:d}'.format(name="seven",age=18)
    print(tp1)

    输出

    i am seven,age18
    tp1 = 'i am {name:s},age{age:d}'.format(**{'name': "seven", 'age': 18})
    print(tp1)

    输出

    i am seven,age18

    :b二进制  :o八进制  :d整  :x十六进制  :%显示百分比(默认小数点后六位)

    多了可以少了不行

    tp1 = 'nambers:{:b},{:o},{:d},{:x},{:x},{:%}'.format(15,15,15,15,15,15.87623,2)
    print(tp1)

    输出

    nambers:1111,17,15,f,f,1587.623000%

    ---恢复内容结束---

  • 相关阅读:
    LinQ&EF任我行(一)LinQ to SQL (转)
    WPF数据模板和控件模板
    Sql优化
    SQL锁表语句
    js动态创建dom
    js实现等待n秒后按钮可用
    js关于事件冒泡
    工作流学习(个人总结)
    sql常用函数
    将Datatable序列化为Json对象返回到客户端
  • 原文地址:https://www.cnblogs.com/liushuizs/p/10265296.html
Copyright © 2011-2022 走看看