zoukankan      html  css  js  c++  java
  • Python %s和%r的区别

    %s 用str()方法处理对象

    %r 用rper()方法处理对象,打印时能够重现它所代表的对象(rper() unambiguously recreate the object it represents)

    例一:

    >>> print("Today is %s" % a)
    Today is sunday
    
    >>> print("Today is %r" % a)
    Today is 'sunday'

    例二:

    >>> content = "What wrong with you, %s" % "Tom"
    
    >>> print("Today is sunday, %s" % content)
    Today is sunday, What wrong with you, Tom
    
    >>> print("Today is sunday, %r" % content)
    Today is sunday, 'What wrong with you, Tom'

    例三:

    >>> import datetime
    >>> d = datetime.date.today()
    
    >>> print("%s" % d)
    2015-04-01
    
    >>> print("%r" % d)
    datetime.date(2015, 4, 1)

    出处:

    http://stackoverflow.com/questions/6005159/when-to-use-r-instead-of-s-in-python

    http://blog.csdn.net/wusuopubupt/article/details/23678291

  • 相关阅读:
    linux echo 换行
    linux 脚本 逻辑关系的写法及区别
    linux vim ***
    跟我一起学Makefile
    linux awk
    linux grep命令 ***
    unbuntu 安装及服务器配置
    linux 静态库文件
    samba 配置
    linux tar
  • 原文地址:https://www.cnblogs.com/zhanhg/p/4384786.html
Copyright © 2011-2022 走看看