zoukankan      html  css  js  c++  java
  • day7-format字符串格式化

    tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
    print(tpl)  # i am seven, age 18, really seven 必须一一对应,否则会报错

    tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) # 要传字典,必须加两个**,否则会报错,作用同上 print(tpl) # i am seven, age 18, really seven,

    tpl = "i am {2}, age {1}, really {0}".format("seven", 18,'alex') print(tpl) # i am alex, age 18, really seven

    tpl = "i am {:s}, age {:d}".format(*["seven", 18]) # *表示传入的是列表,:s指传入的是字符串,:d指传入的是数字 print(tpl) # i am seven, age 18

    tpl = "i am {:s}, age {:d}".format("seven", 18) #["seven", 18] print(tpl) # i am seven, age 18 l=["seven", 18] tpl = "i am {:s}, age {:d}".format('seven',18) print(tpl) # i am seven, age 18 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87623, 2) # :b指二进制 ,:o指8进制,:xX 指16进制 :%指显示成百分比 print(tpl) # numbers: 1111,17,15,f,F, 1587.623000%,2
  • 相关阅读:
    每日总结50
    每日总结49
    每日总结48
    每日总结47
    每日总结46
    每日总结45
    每日总结44
    每日总结42
    每日总结41
    每日总结39
  • 原文地址:https://www.cnblogs.com/mada1027/p/11688645.html
Copyright © 2011-2022 走看看