Python string formatting%r%s

Source: Internet
Author: User

The format character is to reserve a location for the real value and to control the format of the display.

A format character can contain a type code that controls the type of display, as follows:

%s string (shown with STR ())

%r string (shown with repr ())

%c single character

%b binary integers

%d decimal integers

%i Decimal Integer

%o Eight-binary integers

%x hexadecimal integer

%e index (base written as E)

%E index (base written as E)

%f floating Point

%F floating-point number, same as above

%g index (e) or floating point number (depending on display length)

%G Index (E) or floating point number (depending on display length)

Percent% character "%"

add:%i and %d represent signed decimal integers, but %i can automatically convert the octal (or hexadecimal) input into decimal, and %d does not convert.

The difference between STR and repr is supplemented

When Python prints a value, it retains the state of the value in the Python code, not the state that the user wants to see. The print values are different, and print prints the values that the user wants to see.
For example:

>>> "Hello, world!"‘Hello, world!‘   # Python打印出来的值是给python理解的,这里python理解为字符串,所以带着引号>>> 1000L         # python理解为Long型的数字,所以它打印出来的时候也带着后缀L1000L>>> print "Hello, world!"    # 打印一个字符串,给用户看的,所以不带引号Hello, world!>>> print 1000L              # 用户看到的当然是一个数字1000,而不是字符串1000L1000

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

    • Str

      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.

      >>> print str("Hello,  world!")Hello,  world!            >>> print str(1000L)1000                         >>> str("Hello, world!")‘Hello, world!‘               # 字符串转换之后仍然是字符串>>> str(1000L)‘1000‘
    • Repr ()

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

      >>> print repr("Hello,  world!")‘Hello,  world!‘>>> print repr(1000L)1000L>>> repr("Hello,  world!")"‘Hello,  world!‘">>> repr(1000L)‘1000L‘

All in all, the value of STR is a string to look at, repr out the value is to the machine to see, any content in parentheses after it is placed above it plus a layer of quotation marks.

Python string formatting%r%s

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.