"Python④" Python annoying string, formatted output

Source: Internet
Author: User

Annoying strings

A computer can only handle numbers, and if you want to work with text, you must convert the text to a number before processing it. Since the computer was invented by the Americans, only 127 letters were encoded into the computer, that is, letters, numbers, and symbols, which are called ASCII encodings, such as uppercase letters, where the A 65 lowercase letters are a encoded97。

But to deal with the Chinese at least two bytes, but also cannot and ASCII encoding conflict, so, China has developed a GB2312 code to put Chinese into. There are hundreds of languages all over the world, in order to expand the ASCII coding, to display the national language, different countries and regions have set different standards, resulting in GB2312, BIG5, JIS and other coding standards. Countries have national standards, there will inevitably be conflicts, the result is that in multi-language mixed text, the display will be garbled. As a result, Unicode emerges. Unicode unifies all languages into a set of encodings, so there is no more garbled problem.

PS: In this case, the Unicode encoding is not translated into the "variable length code" UTF-8 encoding, interested readers can self-access information.

The representation of a python string

Here the python3.x version of the improvements are very much used.

In the 2.x version:

>>> str="Python 2.x version output Chinese characters are not convenient ">>> str'  Python 2.x \xb0\xe6\xb1\xbe\xca\xe4\xb3\xf6\xd6\xd0\xce\xc4\xd7\xd6\xb7\xfb\xd2\xbb\xb5\xe3\xb6\xbc\xb2\xbb\xb7\ Xbd\xb1\xe3'

So, the code is sometimes caused by a bug can be painful, historical issues, Python 2.x version although Unicode support, but in the syntax ‘xxx‘ and u‘xxx‘ two kinds of string representations.

In the Python 3.x version, the ‘xxx‘ u‘xxx‘被 Unicode encoding, which is write-not-write prefixes, u is the same, and the string represented in bytes must be prefixed with b : b‘xxx‘ .

In the 3.x version:

>>> str="Python 3.x version output Chinese characters very convenient ">>> str'python 3.x version output Chinese characters very convenient '
Formatted output

We will often output like ' Mr. XXX/MS, Hello! Your credit card consumption this month is XXXX yuan, overdraft xxxx Yuan ' such sentence, so we need to format the output.

Common placeholders are:

%%%

formatting integers and floating-point numbers can also specify whether to fill 0 (the last one is rounded).

' %f  '%1.23456789'1.234568'%.3f '%1.23456789'  1.235'

If you are not sure which one to use, %s it will convert any data type to a string.

 >>>  " %s/Madam, Hello! Your credit card consumption this month is%d yuan, overdraft%d yuan   "% ("  Span style= "color: #800000;" > Royal engineering  , 6666,2222)   '  < span> 

Day Summary: Character encoding format, in the 2.x mainstream day is still a pain ah.

"Python④" Python annoying string, formatted output

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.