Python %r和%s區別程式碼例項解析
阿新 • • 發佈:2020-04-05
%r用rper()方法處理物件
%s用str()方法處理物件
相同結果
有些情況下,兩者處理的結果是一樣的,比如說處理int型物件。
例:
print('I am %s years old.' % 22)
print('I am %r years old.' % 22)
返回結果:
I am 22 years old.
I am 22 years old.
不同結果
例:
x = "There are %d types of people." % 10
print('I said: %r' %x)
print('I said: %s' %x)
返回結果
I said: 'There are 10 types of people.' # 通過%r 保留了原有所有屬性
I said: There are 10 types of people.
例:
import datetime riqi = datetime.date.today() print(riqi) print("%s" %riqi) print("%r" %riqi)
返回結果
2020-04-02
2020-04-02
datetime.date(2020,4,2)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。