Python uses str & repr to convert strings _python

Source: Internet
Author: User
Tags in python

may be low or record:

The use of STR and REPR

    1. STR is a type (int, long is similar), and she can also act as a string for a factory method instance
    2. Repr is a python-built function that preserves the true state of a printed value in a Python code fragment

All right, that's all nonsense.

>>> A = 1
>>> A + ""
--------------------------------------------------------------------- ------
TypeError
traceback (most recent) <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 a single string for conversion, not a variable (she does not have the ability to execute variables).

Repr is a function, so it is actually a parameter, it can be a variable and a string

Many people know that Str () can turn 123 numbers into strings, and STR () in Python can even turn lists, dictionaries, and other objects into strings. It's all well understood, but once the Str () and repr () are put together, everyone will be-_-!

Look at a piece of code and still interact in idle:

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

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

Read the first two sentences: English ' hello ' is still ' hello ' after str (), but after repr () it becomes "Hello". This means that Str () returns the string itself, while REPR () returns a string, but it is a standard string, and the official explanation is more winding, let me explain. Repr is the meaning of representation and description, not the description of the person, but the description of the Python machine, which will return something to its description in Python. In one word: repr (obj) tells us what obj is like in the secret, and how it is played by Python in the secretly.

In Python, we are always deceived by our eyes. What is displayed in the editor is not always what it is. Python, for convenience, is always on the surface, secretly another.

To understand the latter two sentences: "Hello" in Chinese after str () into the code ' \xc4\xe3\xba\xc3' , after repr () into the "'\xc4\xe3\xba\xc3 '. ' All with the transfer character, which is equivalent to ' standardize ' the contents of the string. As for ' change ' just to show that repr () returns a new processed string.

After print, str () and repr ()

Look at the code:

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

The previous str (' Hello ') showed the ' \xc4\xe3\xba\xc3 ', and as soon as the print was over, it became the right ' hello '. As mentioned above, the command line directly enters a variable that shows the data it stores in the python background, and what comes out of print displays what is as friendly and visible as possible.
Understanding this, and the difference between the two results of print, is fully understood. Then also give up print as a textual criticism of the 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.