zoukankan      html  css  js  c++  java
  • 多测师讲解内置函数 _format_高级讲师肖sir






    #python中的格式化输出:format()
    # 和%号格式化输出一样,是%号的另外一种格式
    #1、不设置指定位置,按默认顺序

    a ='{}'.format('hello','nihao','dajiaoha')
    print(a) #打印结果 hello ( 默认从左往右)

    a ='{}{}'.format('hello','nihao','dajiaoha')
    print(a)
    2、设置指定索引位置输出

    a ='{2} {1}'.format('hello','nihao','dajiaoha')
    print(a) #打印结果:nihao hello

     

    name = "我叫{1},年龄{2}-{0}-{3}"
    val = name.format('feilong ',"liuyang","99",'88')
    print(val)
    3、设置参数输出

    # # #3、设置参数输出
    d ="姓名:{name} 年纪:{age}".format(name='张三',age=18)
    print(d) #姓名:张三 年纪:18
    3对列表进行格式化

     

     

    4、对元组进行格式化 "*"表示可变长元组

     

    # #大括号里面的0代表的是列表、中括号里面的0和1是列表元素对应的索引位

     

    name = "我叫{0},年龄{1}-{0}-{2}"
    a=(1,2,3)
    # print (type(a))
    val = name.format(*(a))
    print(val)
    # # #3、设置参数输出
    d ="姓名:{name} 年纪:{age}".format(name='张三',age=18)
    print(d) #姓名:张三 年纪:18
    5、对字典进行格式化 "**"表示可变长字典

    #案例1
    # name = "我叫{name},年龄{age}"
    # dic = {'name':'海角','age':18}
    # val = name.format(**dic)
    # print(val)

    #案例2
    a={"name":"多测师","url":"www.duoceshi.com"}
    b="网站名:{name}, 地址:{url}".format(**a)
    print(b)
  • 相关阅读:
    Excel长数字防止转换为科学计数法
    SVN迁移部署
    且行且珍惜
    功能的权衡——推荐功能做不做?
    渗透小白如何学编程
    Metasploit log命令技巧
    Metasploit 使用msfconsole帮助功能技巧
    Metasploit resource命令技巧
    Metasploit makerc命令技巧
    Metasploit irb命令使用技巧
  • 原文地址:https://www.cnblogs.com/xiaolehua/p/13695976.html
Copyright © 2011-2022 走看看