Python uses str & repr to convert the string, pythonstr

Source: Internet
Author: User

Python uses str & repr to convert the string, pythonstr

It may be better to record low:

Use of str and repr

  1. Str is a type (int, long is similar). It can also be used as a string for a factory Method Instance.
  2. Repr is a built-in function of python that retains the real status of a print value in a python code snippet.

Okay. All the above is nonsense.

>>> a = 1>>> a + ""---------------------------------------------------------------------------TypeErrorTraceback (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) + ""

We can see that we can use str and repr to convert strings.

However, str can only provide one meta string for conversion. It cannot be a variable (she does not have the ability to execute variables)

Repr is a function, so it is actually a passing parameter. It can be a variable or a string.

Many people know that str () can convert 123 numbers into strings, and str () in python can even convert objects such as lists and dictionaries into strings. This is easy to understand, but once str () and repr () are put together, everyone will not be calm -_-!

Let's look at a piece of code, which is still interactive in IDLE:

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

First, let's look at the first two sentences: 'hello' after str () is still 'hello', but after repr () is changed to "'hello '". This indicates that str () returns the string itself, while repr () returns the string, but it is a standard string. The official explanation is similar. Let me explain it. Repr refers to representation and description. It is not a human description, but a description of the python machine, that is, it will return a description of something in python. To put it bluntly: repr (obj) tells us what the obj variable looks like in the back, and how it is processed by python in the back.

In python, we are always deceived by our eyes. What is displayed in the editor is not always its original appearance. For convenience, python is always one set on the surface and one set on the back.

Let's take a look at the next two sentences: Chinese 'hello' after str () becomes the encoding'\xc4\xe3\xba\xc3'After repr () is changed"'\xc4\xe3\xba\xc3'". 'Added the escape character to \, which is equivalent to "standardizing" the content in the string. As for 'to "', it is only to indicate that repr () returns a new processed string.

Str () and repr () after print ()

Let's look at the Code:

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

Previously, str ('hello') showed '\ xc4 \ xe3 \ xba \ xc3', and after print, it became correct 'Hello '. As mentioned above, directly input a variable in the command line to display the data stored in the python background, it will show things that are as friendly and understandable as possible.
After understanding this, we fully understand the differences between the two print results. Then I gave up print as my research heart.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.