zoukankan      html  css  js  c++  java
  • Day1 字符串格式化

    1.占位符方式:
    占位符:
      %d   整数   %012d   数字位数至少长度为12位,不足的前面加0填充。
                    >>> 'Hello,%s,%012d' % ('a',1234567890123456)
                    'Hello,a,1234567890123456'
                    >>> 'Hello,%s,%012d' % ('a',123)
                    'Hello,a,000000000123'

      %f   浮点数   %.4f   小数点后保留2位,超出两位的四舍五入,不足两位的用0占位。
                    >>> 'Hello,%s,%.4f' % ('a',123.1234567)
                    'Hello,a,123.1235'
                    >>> 'Hello,%s,%.4f' % ('a',123.1)
                    'Hello,a,123.1000'

      %s   字符串       可以把任何类型转换为字符串,不确定用什么的时候%s永远好用。
                    在字符串中,需要输出%符号时, 用%转义, 通过%%来表示%符号。

      %x   十六进制数

    有几个占位符,后面就跟几个变量或值,顺序一一对应, 如:

        >>> 'Hello,%s' %'world'
        'Hello,world'
        >>> 'Hello,%s,%s' % ('world','worl2')
        'Hello,world,worl2'
        >>> 'Hello,%s,%s,%d' % ('world','worl2',120)
        'Hello,world,worl2,120'
        >>> 'Hello,%s,%s,%d,%f' % ('world','worl2',120,3.14)
        'Hello,world,worl2,120,3.140000'

    2.format()函数方式
    format()函数
      使用传入函数的参数,依次替换字符串内的顺序占位符, 略麻烦。

        >>> 'Hello,{0},{1},{2},hahahaha,{3},{4:05d},{5:.4f},end'.format('a',666,'aaa%%',22,33,1.23456)
        'Hello,a,666,aaa%%,hahahaha,22,00033,1.2346,end'

    个个原创文章

    欢迎讨论
    https://www.cnblogs.com/konggg/
    欢迎转载收藏,转载请注明来源,谢谢支持!
  • 相关阅读:
    SQL Server 中的事务与事务隔离级别以及如何理解脏读, 未提交读,不可重复读和幻读产生的过程和原因
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSAS 系列
    微软BI 之SSRS 系列
    微软BI 之SSRS 系列
    配置 SQL Server Email 发送以及 Job 的 Notification通知功能
  • 原文地址:https://www.cnblogs.com/konggg/p/8945745.html
Copyright © 2011-2022 走看看