看代码的时候一头雾水,不清楚什么情况下使用:
print "It write: %s." % qprint "It write: %r." % q
这二者有什么区别吗?
解决方案
%r是repr
%s就是str
比如\x27是单引号>>> print '%r'%'\x27' # 带括号的单引号"'" >>> print '%s'%'\x27' # 纯单引号'>>> class Example(object): ... __repr__ = lambda cls: '<Demo>(repr)'... __str__ = lambda cls: '<Demo>(str)'... >>> example = Example()>>> print '%s'%example<Demo>(str)>>> print '%r'%example<Demo>(repr)