python converts strings using str repr

  • 2020-05-12 02:49:32
  • OfStack

May compare low or record 1:

The usage of str and repr

str is a type (int, long is similar), and it can also be used as an instance of a factory method with an string repr is a built-in function of python that preserves the true state of one printed value in the python code snippet

All right, that's bullshit


>>> a = 1
>>> a + ""
---------------------------------------------------------------------------
TypeError
Traceback (most recent call last)
<ipython-input-5-ebf3ab7f3a34> in <module>()
----> 1 a + ""

TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> a = 1
>>> repr(a) + ""

As you can see, we can convert strings by using str and repr

However, str can only provide 1 meta string for conversion, not 1 variable (she does not have the ability to execute variables)

repr is a function, so it's actually a parameter, it could be a variable and string

Most people know that str() turns 123 Numbers into strings, and str() in python even turns lists, dictionaries, and other objects into strings. This is easy to understand, but 1 once put str() and repr() in 1, we are all not calm!

Take a look at 1 piece of code, still interacting in IDLE:


  >>> str('hello')
  'hello'
  >>> repr('hello')
  "'hello'"

  >>> str(' hello ')
  '\xc4\xe3\xba\xc3'
  >>> repr(' hello ')
  "'\\xc4\\xe3\\xba\\xc3'"

In English, 'hello' is still 'hello' after 'str()', but it becomes 'hello' after 'repr()'. This means that str() returns the string itself, and repr() returns a string, but it's a standard string. repr is representation and describes, not a description of a person, but a description of an python machine, that is, it will return something to a description of it in python. Speak for yourself: repr(obj) tells us what the variable obj looks like in the background and how it is handled by python in the background and "played" by python.

In python, we are always deceived by our eyes. What is displayed in an editor is not always what it should be. For convenience, python always has one on the surface and one behind closed doors.

The Chinese word 'ni hao' is encoded after str(). \xc4\xe3\xba\xc3' After repr() "'\xc4\xe3\xba\xc3 '". 'is changed to \, which "normalizes" everything in the string. As for 'becomes' just to show that repr() returns a new string that has been processed.

str() and repr() after print

Look at the code:


  >>> print str(' hello ')
   hello 
  >>> print repr(' hello ')
  '\xc4\xe3\xba\xc3'

The previous str(' hello ') display was '\xc4\xe3\xba\xc3', while the 1 goes through print and becomes the correct 'hello'. As mentioned above, enter a variable directly from the command line to display the data it stores in the background of python. What comes out of print will appear as friendly and understandable as possible.
Once you understand this, you understand the difference between the two results of print. Then I gave up print as the core of textual criticism.


Related articles: