zoukankan      html  css  js  c++  java
  • python format

      

    >>> "{0} love {1}.{2}".format("I","You","com")
    'I love You.com'
    >>>

    >>> "{a} love {b}.{c}".format("I","You","com")
    Traceback (most recent call last):
    File "<pyshell#1>", line 1, in <module>
    "{a} love {b}.{c}".format("I","You","com")
    KeyError: 'a'
    >>> "{a} love {b}.{c}".format(a="I",b="You",c="com")
    'I love You.com'  //需要a为关键字
    >>>

    >>> print(" a")  //转义
    a
    >>> print("\")

    >>> "{{0}}".format("不打印")
    '{0}'

    "{0.1f}{1}".format(27.658,'GB')
    AttributeError: 'float' object has no attribute '1f'
    >>> "{0:.1f}{1}".format(27.658,'GB')  //四舍五入
    '27.7GB'
    >>>

    >>> '%c' % 97  //a的ascii码为97
    'a'
    >>>

    >>> "%c" "%c" "%c" % (97,98,99) //格式化ascii码
    'abc'
    >>> "%c %c %c" % (97,98,99)  
    'a b c'
    >>>

    >>> "%d + %d = %d" % (4,5,4+5)  //格式化数字
    '4 + 5 = 9'
    >>> "%s" % " i love you" //格式化字符串
    ' i love you'
    >>>

  • 相关阅读:
    虚拟机Centos安装docker小记
    Python selenium入门
    selenium Error
    DveOps路线指南
    DevOps
    Go语言常量和变量
    安装Go语言及环境的搭建
    Win10 搭建IIS服务
    linux 上搭建sftp服务
    linux小命令
  • 原文地址:https://www.cnblogs.com/xiaohouzai/p/7642394.html
Copyright © 2011-2022 走看看