• python字符串格式化


    Python 2.7.12rc1 (v2.7.12rc1:13912cd1e7e8, Jun 12 2016, 05:57:31) [MSC v.1500 64 bit (AMD64)] on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> str1="i love fish"
    >>> str1[:6]
    'i love'
    >>> str="111eee"
    >>> str.isalnum()
    True
    >>> str6="i love cppc"
    >>> str
    '111eee'
    >>> str6
    'i love cppc'
    >>> str6.splice()
    
    Traceback (most recent call last):
      File "<pyshell#7>", line 1, in <module>
        str6.splice()
    AttributeError: 'str' object has no attribute 'splice'
    >>> str6.split()
    ['i', 'love', 'cppc']
    >>> str6
    'i love cppc'
    >>> str6.title()
    'I Love Cppc'
    >>> "{a} love {b}.{c}".format(a='i',b='fish',c='com')
    'i love fish.com'
    >>> {0}.format("ni")
    
    Traceback (most recent call last):
      File "<pyshell#12>", line 1, in <module>
        {0}.format("ni")
    AttributeError: 'set' object has no attribute 'format'
    >>> "{0}".format("ni")
    'ni'
    >>> {{0}}.format("ni")
    
    Traceback (most recent call last):
      File "<pyshell#14>", line 1, in <module>
        {{0}}.format("ni")
    TypeError: unhashable type: 'set'
    >>>  "{{0}}".format("ni")
     
      File "<pyshell#15>", line 2
        "{{0}}".format("ni")
        ^
    IndentationError: unexpected indent
    >>> "{}".format("ni")
    'ni'
    >>> "{{0}}".format("ni")
    '{0}'
    >>> "{0:.1lf}{1}".format(27.546,GB)
    
    Traceback (most recent call last):
      File "<pyshell#18>", line 1, in <module>
        "{0:.1lf}{1}".format(27.546,GB)
    NameError: name 'GB' is not defined
    >>> "{0:.1lf}{1}".format('27.546','GB')
    
    Traceback (most recent call last):
      File "<pyshell#19>", line 1, in <module>
        "{0:.1lf}{1}".format('27.546','GB')
    ValueError: Invalid conversion specification
    >>> "{0:.1lf}{1}".format(27.546,'GB')
    
    Traceback (most recent call last):
      File "<pyshell#20>", line 1, in <module>
        "{0:.1lf}{1}".format(27.546,'GB')
    ValueError: Invalid conversion specification
    >>> "{0:.1f}{1}".format('27.546','GB')
    
    Traceback (most recent call last):
      File "<pyshell#21>", line 1, in <module>
        "{0:.1f}{1}".format('27.546','GB')
    ValueError: Unknown format code 'f' for object of type 'str'
    >>> '{0:.1lf}{1}'.format(27.546,'GB')
    
    Traceback (most recent call last):
      File "<pyshell#22>", line 1, in <module>
        '{0:.1lf}{1}'.format(27.546,'GB')
    ValueError: Invalid conversion specification
    >>> "{0:.lf}{1}".format(27.546,'GB')
    
    Traceback (most recent call last):
      File "<pyshell#23>", line 1, in <module>
        "{0:.lf}{1}".format(27.546,'GB')
    ValueError: Format specifier missing precision
    >>> "{0:.1f}{1}".format(27.546,'GB')
    '27.5GB'
    >>> '%c %c %c' % (97,97,99)
    'a a c'
    >>> '%e'  %  27.3342
    '2.733420e+01'
    >>> 

  • 相关阅读:
    Centos7端口查看相关
    Redis线程安全问题
    利用redis 分布式锁 解决集群环境下多次定时任务执行
    分布式锁的几种实现方式~
    什么是 CI/CD? (持续集成/持续交付)
    Java JDBC 中获取 ResultSet 的大小
    BeetleX之XRPC使用详解
    事件驱动框架EventNext之线程容器
    BeetleX之快速构建Web多房间聊天室
    BeetleX之TCP服务应用详解
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/6045332.html
Copyright © 2020-2023  润新知