Python converts strings using str & REPR

Source: Internet
Author: User
may be low or record:

Use of STR and REPR

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

All right, that's all crap.

>>> A = 1>>> A + ""--------------------------------------------------------------------------- Typeerrortraceback (most recent call last) in
 
   
  
   
    
   ()----> 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 meta 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 objects such as lists, dictionaries, and so on into strings. This is all well understood, but once Str () and repr () are put together, everyone is not calm-_-!

Look at a piece of code that is still interacting in idle:

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

Look at the first two sentences: the 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 a bit more around, 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. The word: repr (obj) tells us what the obj variable looks like in the secret, and how it is being "toyed" with by Python in the secretly.

In Python, we are always deceived by our eyes. What is displayed in the editor is not always its original face. Python, for convenience, is always on the surface, behind a set of secretly.

Again to understand the second sentence: The Chinese ' hello ' after str () became the code ' \xc4\xe3\xba\xc3' , after Repr () became a "'\xc4\xe3\xba\xc3 ' ". ' All adds the transfer character to \, which is equivalent to "normalize" the contents of the string. As for ' become ' just to show that Repr () is returning a new processed string.

STR () and repr () after print

Look at the code:

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

Before str (' hello ') shows ' \xc4\xe3\xba\xc3 ', and a print, it becomes the right ' hello '. As stated above, the command line directly enters a variable that shows the data it stores in the background of the Python, and the print out will show something as friendly and readable as possible.
Understanding this, the difference between the two results of print is completely understood. Then he gave up print as textual research's 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.