Python艰难的方式 - 练习6 - %r与%s

时间:2021-01-09 18:10:18

http://learnpythonthehardway.org/book/ex6.html

http://learnpythonthehardway.org/book/ex6.html

Zed seems to use %r and %s interchangeably here, is there any difference between the two? Why not just use %s all the time?

Zed似乎在这里可以互换地使用%r和%s,两者之间有什么区别吗?为什么不一直只使用%s?

Also, I wasn't sure what to search for in the documentation to find more info on this. What are %r and %s called exactly? Formatting strings?

此外,我不知道在文档中搜索什么以查找更多信息。什么是%r和%s完全被称为?格式化字符串?

7 个解决方案

#1


54  

They are called string formatting operations.

它们被称为字符串格式化操作。

The difference between %s and %r is that %s uses the str function and %r uses the repr function. You can read about the differences between str and repr in this answer, but for built-in types, the biggest difference in practice is that repr for strings includes quotes and all special characters are escaped.

%s和%r之间的区别在于%s使用str函数,%r使用repr函数。你可以在这个答案中读到str和repr之间的区别,但是对于内置类型,实践中最大的区别是字符串的repr包含引号,所有特殊字符都被转义。

#2


22  

%r calls repr, while %s calls str. These may behave differently for some types, but not for others: repr returns "a printable representation of an object", while str returns "a nicely printable representation of an object". For example, they are different for strings:

%r调用repr,而%s调用str。对于某些类型,这些可能表现不同,但对于其他类型则不然:repr返回“对象的可打印表示”,而str返回“对象的可打印表示”。例如,它们对于字符串是不同的:

>>> s = "spam"
>>> print(repr(s))
'spam'
>>> print(str(s))
spam

In this case, the repr is the literal representation of a string (which the Python interpreter can parse into a str object), while the str is just the contents of the string.

在这种情况下,repr是字符串的文字表示(Python解释器可以解析为str对象),而str只是字符串的内容。

#3


2  

%s invokes str(), whereas %r invokes repr(). For details, see Difference between __str__ and __repr__ in Python

%s调用str(),而%r调用repr()。有关详细信息,请参阅Python中__str__和__repr__之间的区别

#4


2  

The following is a summary of the preceding three code examples.

以下是前三个代码示例的摘要。

# First Example
s = 'spam'
# "repr" returns a printable representation of an object,
# which means the quote marks will also be printed.
print(repr(s))
# 'spam'
# "str" returns a nicely printable representation of an
# object, which means the quote marks are not included.
print(str(s))
# spam

# Second Example.
x = "example"
print ("My %r" %x)
# My 'example'
# Note that the original double quotes now appear as single quotes.
print ("My %s" %x)
# My example

# Third Example.
x = 'xxx'
withR = ("Prints with quotes: %r" %x)
withS = ("Prints without quotes: %s" %x)
print(withR)
# Prints with quotes: 'xxx'
print(withS)
# Prints without quotes: xxx

#5


0  

 x = "example"
 print "My %s"%x

       My example

 print "My %r"%x

       My 'example'

It is well explained in the above answers. I have tried to show the same with a simple example.

在上面的答案中有很好的解释。我试图通过一个简单的例子来展示它。

#6


0  

%s => string

%s =>字符串

%r => exactly as is

%r =>完全一样

Using the code in the book:

使用书中的代码:

my_name = 'Zed A. Shaw'
print "Let's talk about %s." % my_name
print "Let's talk about %r." % my_name

we get

我们得到

Let's talk about Zed A. Shaw.
Let's talk about 'Zed A. Shaw'.

#7


-1  

The code below illustrates the difference. Same value prints differently:

下面的代码说明了差异。相同值打印方式不同:

x = "xxx"
withR = "prints with quotes %r"
withS = "prints without quotes %s"

#1


54  

They are called string formatting operations.

它们被称为字符串格式化操作。

The difference between %s and %r is that %s uses the str function and %r uses the repr function. You can read about the differences between str and repr in this answer, but for built-in types, the biggest difference in practice is that repr for strings includes quotes and all special characters are escaped.

%s和%r之间的区别在于%s使用str函数,%r使用repr函数。你可以在这个答案中读到str和repr之间的区别,但是对于内置类型,实践中最大的区别是字符串的repr包含引号,所有特殊字符都被转义。

#2


22  

%r calls repr, while %s calls str. These may behave differently for some types, but not for others: repr returns "a printable representation of an object", while str returns "a nicely printable representation of an object". For example, they are different for strings:

%r调用repr,而%s调用str。对于某些类型,这些可能表现不同,但对于其他类型则不然:repr返回“对象的可打印表示”,而str返回“对象的可打印表示”。例如,它们对于字符串是不同的:

>>> s = "spam"
>>> print(repr(s))
'spam'
>>> print(str(s))
spam

In this case, the repr is the literal representation of a string (which the Python interpreter can parse into a str object), while the str is just the contents of the string.

在这种情况下,repr是字符串的文字表示(Python解释器可以解析为str对象),而str只是字符串的内容。

#3


2  

%s invokes str(), whereas %r invokes repr(). For details, see Difference between __str__ and __repr__ in Python

%s调用str(),而%r调用repr()。有关详细信息,请参阅Python中__str__和__repr__之间的区别

#4


2  

The following is a summary of the preceding three code examples.

以下是前三个代码示例的摘要。

# First Example
s = 'spam'
# "repr" returns a printable representation of an object,
# which means the quote marks will also be printed.
print(repr(s))
# 'spam'
# "str" returns a nicely printable representation of an
# object, which means the quote marks are not included.
print(str(s))
# spam

# Second Example.
x = "example"
print ("My %r" %x)
# My 'example'
# Note that the original double quotes now appear as single quotes.
print ("My %s" %x)
# My example

# Third Example.
x = 'xxx'
withR = ("Prints with quotes: %r" %x)
withS = ("Prints without quotes: %s" %x)
print(withR)
# Prints with quotes: 'xxx'
print(withS)
# Prints without quotes: xxx

#5


0  

 x = "example"
 print "My %s"%x

       My example

 print "My %r"%x

       My 'example'

It is well explained in the above answers. I have tried to show the same with a simple example.

在上面的答案中有很好的解释。我试图通过一个简单的例子来展示它。

#6


0  

%s => string

%s =>字符串

%r => exactly as is

%r =>完全一样

Using the code in the book:

使用书中的代码:

my_name = 'Zed A. Shaw'
print "Let's talk about %s." % my_name
print "Let's talk about %r." % my_name

we get

我们得到

Let's talk about Zed A. Shaw.
Let's talk about 'Zed A. Shaw'.

#7


-1  

The code below illustrates the difference. Same value prints differently:

下面的代码说明了差异。相同值打印方式不同:

x = "xxx"
withR = "prints with quotes %r"
withS = "prints without quotes %s"