Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does !r do in str() and repr()?

According to the Python 2.7.12 documentation:

!s (apply str()) and !r (apply repr()) can be used to convert the value before it is formatted.

>>> import math >>> print 'The value of PI is approximately {}.'.format(math.pi) The value of PI is approximately 3.14159265359. >>> print 'The value of PI is approximately {!r}.'.format(math.pi) The value of PI is approximately 3.141592653589793. 

Interestingly, the converted value is the output of repr(), rather than str().

>>> str(math.pi) '3.14159265359' >>> repr(math.pi) '3.141592653589793' 

So what does "convert the value" mean here? Making it less human-readable?

like image 855
nalzok Avatar asked Jul 17 '16 05:07

nalzok


People also ask

What does R do in Python string?

r means the string will be treated as raw string. See the official Python 2 Reference about "String literals": When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string.

What is r in Python string format?

Python raw string is created by prefixing a string literal with 'r' or 'R'. Python raw string treats backslash (\) as a literal character. This is useful when we want to have a string that contains backslash and don't want it to be treated as an escape character.

What is the difference between repr () or str () function?

repr() compute the “official” string representation of an object (a representation that has all information about the object) and str() is used to compute the “informal” string representation of an object (a representation that is useful for printing the object).

What is the purpose of defining the functions __ str __ and __ repr __ within a class how are the two functions different?

Summary. Both __str__ and __repr__ functions return string representation of the object. The __str__ string representation is supposed to be human-friendly and mostly used for logging purposes, whereas __repr__ representation is supposed to contain information about object so that it can be constructed again.


2 Answers

In order to format something in a string, a string representation of that something must first be created. "convert the value" is basically talking about how the string representation is to be constructed. In python, there are two fairly natural choices to get a string representation of something ... str and repr. str is generally a little more human friendly, repr is generally more precise. Perhaps the official documentation is the best place to go looking for the difference:

object.__repr__(self)

  • Called by the repr() built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned. The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an “informal” string representation of instances of that class is required.

  • This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.

object.__str__(self)

  • Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.

  • This method differs from object.__repr__() in that there is no expectation that __str__() return a valid Python expression: a more convenient or concise representation can be used.

  • The default implementation defined by the built-in type object calls object.__repr__().

In str.format, !s chooses to use str to format the object whereas !r chooses repr to format the value.

The difference can easily be seen with strings (as repr for a string will include outer quotes).:

>>> 'foo {}'.format('bar') 'foo bar' >>> 'foo {!r}'.format('bar') "foo 'bar'" 

What the difference between these two methods really depends critically on the objects being formatted. For many objects (e.g. those that don't override the __str__ method), there will be no difference in the formatted output.

like image 149
mgilson Avatar answered Oct 22 '22 06:10

mgilson


I called the str(object) and the function -format() and print() to compute the as someone called it “informal”.

To perfectly print string representation of an object. The return value must be a string object as well.

like image 31
Alg2code Avatar answered Oct 22 '22 05:10

Alg2code