zoukankan      html  css  js  c++  java
  • format格式化函数

    format格式化

    # ^,<,>分别表示居中,左对齐,右对齐;冒号后面是要填充的字符,只能是一个字符,不指定默认为空格
    print('{:*^60}'.format('token & word mapping review:'))
    print('{:.2f}'.format(3.1415926))
    # 使用大括号{}转义大括号
    print('{}对应的位置是:{{0}}'.format('runoob'))
    runoob对应的位置是:{0}

    format函数接受不限个参数,位置不按顺序

    print('{}{}'.format('hello', 'world'))    # 不设定位置,默认按顺序
    print('{0},{1}'.format('hello', 'world'))  # 设定顺序
    print('{1}-{0}-{1}'.format('hello', 'world'))
    helloworld
    hello,world
    world-hello-world

    format设置参数

    print('网站名:{name}, 地址{url}'.format(name='菜鸟教程',url='www.runoob.com'))
    # 通过字典设置参数
    site = {'name':'菜鸟教程', 'url':'www.runoob.com'}
    print('网站名:{name}, 地址:{url}'.format(**site))
    网站名:菜鸟教程, 地址:www.runoob.com

    # 通过列表索引设置参数
    my_list = ['菜鸟教程', 'www.runoob.com']
    print('网站名:{0[0]}, 地址:{0[1]}'.format(my_list))

     注意:列表索引设置参数,‘0’是必须的。

  • 相关阅读:
    图片滚动
    DOM 练习
    HTML 求阶乘之和
    JavaScript 累加求和练习 函数
    JavaScript 累加求和练习
    JavaScript
    汽车之家官网首页排版与布局
    网页搜索页面排版布局
    转---Python——numpy random类
    转---redshift database ---学习
  • 原文地址:https://www.cnblogs.com/keye/p/9252499.html
Copyright © 2011-2022 走看看