python str()、repr()、asiic()
!s:str(),返回对象的字符串表示形式,如果字符串是已编码,返回的是解码后的字符串。转换过后的字符串最外层不带引号,如 abc
!r:repr(),返回对象的规范字符串表示形式。转换过后的字符串最外层带引号,如 'abc'
注意:对于许多对象类型,包括大多数内建,eval(repr(obj)) == obj
!a:ascii(),返回对象的纯 ascii 表示形式,但要使用\\x、\\u或\\U转义转义所返回字符串中的非 ascii 字符。
示例:
str_ = "12aA中国"
a = ascii(str_) # '12aA\u4e2d\u56fd'
b = repr(str_) # '12aA中国'
c = str('\u56fd') # 国
d = repr('\u56fd') # '国'
e = eval(b) # 12aA中国
t = "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')
print(t) # repr() shows quotes: 'test1'; str() doesn't: test2
t2 = "repr() shows quotes: {0!r}; str() doesn't: {1!s}".format('test1', 'test2')
print(t2) # repr() shows quotes: 'test1'; str() doesn't: test2
str(str_) == str_ # True
repr(str_) == str_ # False
len(str(str_)) # 6
len(repr(str_)) # 8