zoukankan      html  css  js  c++  java
  • py-day2-6 python format字符串格式化

    # format() 方法  {}代替元素   默认是从左往右开始取值
    test = 'i am {},age {},{}'.format('xiaoma',18,'happy')
    print(test)
    
    i am xiaoma,age 18,happy
    
    test1 = 'i am {1},age {0},{2}'.format('18','xiaoma','happy')
    print(test1)
    
    i am xiaoma,age 18,happy
    
    test2 = 'i am {0},age {0},{0}'.format('18','xiaoma','happy')
    print(test2)
    
    i am 18,age 18,18
    
    # 字典取值   .format(*[]) 取列表   .format(**{}) 取字典
    test3 = 'i am {name},age {age},{city}'.format(name ='xiaoma',age=18,city='china')
    print(test3)
    test3_1 = 'i am {name},age {age},{city}'.format(**{'name':'xiaoma','age':18,'city':'shanhai'})
    print(test3_1)
    
    i am xiaoma,age 18,china
    i am xiaoma,age 18,shanhai
    
    # 用{:}取指定类型值
    test4 = 'i am {:s},age {:d},{:f}'.format('xiaoma',18,3.1415)
    print(test4)
    
    i am xiaoma,age 18,3.141500
    
    # 进制转换 二进制,八进制,十进制···
    t = 'number: {:b},{:o},{:d},{:x},{:X},{:%}'.format(15,15,15,15,15,3.1415926)
    print(t)
    
    number: 1111,17,15,f,F,314.159260%
  • 相关阅读:
    Data Security---->Control Access to the Organization
    Data Modeling
    Slaesforce Paltform Development Basic
    Customize your Chatter Experience.
    wamp自定义网站根目录及多站点配置
    1053-1055
    1046-1052
    1044-1045
    HDOJ 1038-1043
    HDOJ 1031-1037
  • 原文地址:https://www.cnblogs.com/majunBK/p/10423692.html
Copyright © 2011-2022 走看看