The string in Python represents str and repr

Source: Internet
Author: User

All strings printed in Python are enclosed in quotation marks. This is because when Python prints the value, it keeps the value in the Python code state, not the state you want the user to see, and if you use the print statement, the result is different.

>>> "Hello,world"
The ' hello,world ' # python-printed value is understood by Python, where Python is understood as a string, so enclose the quotation marks

>>> print "Hello,world"
Hello,world
>>>

*************************************************************************************************************** *

>>> 10000L
10000L # Python is understood as a long number, so it prints out with the suffix l

>>> Print 10000L # The user sees of course a number 1000, not a string of 1000L

10000
>>>

STR and repr actually correspond to each of the above two display modes.

Converts a value into a reasonable form of a string to be seen by the user. STR is actually similar to Int,long, and is a type.

Repr ()

Creates a string that represents a value in the form of a legitimate Python expression. Repr () is a function.

As you can see here, the long integer 10000L is converted to the number 10000, and also when displayed to the user, when you want to know what the value of a variable is, you may be interested in whether it is an integer or a long integer type.

The two mechanisms by which values are converted to strings. The two mechanisms can be used with the following two functions: one is through the STR function, which converts the value into a reasonable form of string for the user to understand, and the other through the REPR function, which creates a string representing the value in the form of a valid Python expression.

Take a look at the following example:

>>> print repr ("Hello,world")
' Hello,world '
>>> print repr (1000L)
1000L
>>> Print str ("Hello,world")
Hello,world
>>> Print str (1000L)
1000


REPR (x) can also write the ' X ' implementation (note that ' is an anti-quote, not a single quote). If you want to print a sentence that contains numbers, the anti-quote is useful. Like what:

>>> temp = 42
>>> print "The temperature is" + Temp
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:cannot concatenate ' str ' and ' int ' objects
>>> print "The temperature is" + ' temp '
The temperature is 42
>>>

>>> print "The temperature is" + str (temp)
The temperature is 42
>>> print "The temperature is" + repr (temp)
The temperature is 42
>>>

The first print statement does not work, because it is not possible to add strings and numbers. The second one works correctly, thinking that the value of temp has been converted to the string "42" by an anti-quote. (You can use repr, of course, to get the same result)

In short, str,repr and anti-quotes are 3 ways to convert Python values to strings. The function str makes the string easier to read, while REPR (and anti-quotes) converts the resulting string into a valid Python expression. The value of STR is a string to be seen, and the value of repr is given to the machine, and any content in parentheses comes out on top of it with a layer of quotation marks.






The string in Python represents str and repr

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.