熱線電話:13121318867

登錄
首頁精彩閱讀python中%r和%s的區別
python中%r和%s的區別
2018-03-04
收藏

python中%r和%s的區別

%r用rper()方法處理對象
%s用str()方法處理對象
有些情況下,兩者處理的結果是一樣的,比如說處理int型對象。
例一:
[python] view plain copy

    print "I am %d years old." % 22  
    print "I am %s years old." % 22  
    print "I am %r years old." % 22  
返回結果:
[python] view plain copy

    I am 22 years old.  
    I am 22 years old.  
    I am 22 years old.  
另外一些情況兩者就不同了
例二:
[python] view plain copy

    text = "I am %d years old." % 22  
    print "I said: %s." % text  
    print "I said: %r." % text  

返回結果:
[python] view plain copy
    I said: I am 22 years old..  
    I said: 'I am 22 years old.'. // %r 給字符串加了單引號  
再看一種情況
例三:
[python] view plain copy

    import datetime  
    d = datetime.date.today()  
    print "%s" % d  
    print "%r" % d  
返回結果:
[python] view plain copy

    2014-04-14  
    datetime.date(2014, 4, 14)  
可見,%r打印時能夠重現它所代表的對象(rper() unambiguously recreate the object it represents)


數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢