[Python 4] python annoying strings, formatting output, and python annoying

Source: Internet
Author: User

[Python 4] python annoying strings, formatting output, and python annoying
Annoying string

The computer can only process numbers. If you want to process text, you must convert the text into numbers before processing. Since computers were invented by Americans, only 127 letters were first encoded into computers, that is, uppercase and lowercase English letters, numbers, and symbols.ASCIIEncoding, such as uppercase lettersAThe encoding is65, Lowercase lettersaThe encoding is97。

However, it takes at least two bytes to process Chinese characters and cannot conflict with ASCII codes.GB2312Encoding used to compile Chinese characters. There are hundreds of languages around the world. In order to expand the ASCII code and display their own languages, different countries and regions have developed different standards, resulting in GB2312, BIG5, JIS and other respective encoding standards. The standards of various countries will inevitably conflict with each other. As a result, garbled characters are displayed in multi-language texts. Therefore, Unicode came into being. Unicode unifies all languages into a set of encodings, so that there will be no garbled issues.

Ps: Unicode encoding is converted to variable-length encoding.UTF-8Encoding. If you are interested, you can check the information on your own.

Representation of python strings

Here, improvements to Python 3.x are very useful.

In 2.x:

>>> Str = "it is inconvenient to output Chinese Characters in python 2.x" >>> 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' >>>

Therefore, sometimes the BUG caused by encoding can be confusing, and the legacy problems of history. Although Python 2.x supports Unicode, it is required in syntax.'xxx'Andu'xxx'String representation.

In Python 3. x,'xxx'AndU'xxx' isUnified into Unicode encoding, that is, do not write the prefixuAll are the same, while strings in bytes must be addedbPrefix:b'xxx'.

In version 3.x:

>>> Str = "it is convenient to output Chinese Characters in python 3.x" >>> str 'ease of output Chinese Characters in python 3.x '>>>
Format output

We often output a message like 'xxx, Mr/Ms. Hello! Your credit card consumption amount for this month is XXXX yuan, overdraft XXXX yuan, so we need to format the output.

Common placeholders include:

% D integer % f floating point % s string % x hexadecimal integer

To Format integers and floating-point numbers, you can also specify whether to add 0 (rounding the last digit ).

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

If you are not sure which one to use, you can use%s, Which converts any data type to a string.

>>> Hello, Mr./Ms. % s! Your credit card amount for this month is % d yuan, overdraft % d yuan '% ('Royal Polytechnic', 6666,2222) 'Hello, Mr/Ms Royal Polytechnic! Your credit card consumption amount for this month is 6666 yuan, overdraft 2222 yuan '>' % s Mr/Ms, hello! Your credit card consumption amount for this month is % s yuan, overdraft % s yuan '% ('Royal Polytechnic', 6666,2222) 'Hello, Mr/Ms Royal Polytechnic! Your credit card consumption amount for this month is 6666 yuan, overdraft 2222 yuan '>

 

Summary of the day: the character encoding format is still a pain in the 2.x mainstream days.

 


Format python strings

In python, the output tag is similar to the printf () format in c. In python, the % operator is used to format the output string. The common format is
Format mark string % value group to be output
The "format mark string" on the Left can be exactly the same as that in c. If there are two or more values in the 'value Group' on the right, they must be enclosed in parentheses and separated by short signs. Focus on the left part. The simplest form of the left part is:
% Cdoe
There are many types of codes, but in python, everything can be converted to the string type. Therefore, if there are no special requirements, you can use '% s' to mark them all. For example:
'% S % s' % (1, 2.3, ['one', 'two', 'three'])
Its output is '1 2.3 ['one', 'two', 'three] ', which is output according to the mark on the left of %. Although the first and second values are not of the string type, there is no problem. In this process, when the computer finds that the first value is not % s, it will first call the integer function to convert the first value, that is, 1, to the string type, then, call the str () function to output the data. As mentioned above, there is also a repr () function. If you want to use this function, you can mark it with % r. In addition to % s, there are many similar codes:
Integer: % d
Unsigned integer: % u
Octal: % o
Hexadecimal: % x % X
Floating Point: % f
Scientific Note: % e % E
Automatically select % e or % f: % g based on different values
Automatically select % E or % f: % G based on different values
As mentioned above, escape with "\" is the same. Here, % is used as the mark of the format, and there is also a question about how % should be output. If you want to output % itself in the format mark string, % can be used.
The above is just the simplest form of format mark. It is more complex:
'% 6.2f' % 1.235
In this form, a decimal point 6.2 is displayed before f, which indicates that the total output length is 6 characters, with two decimal places. More complex:
'% 06.2f' % 1.235
A value of 0 is added before 6, indicating that if the number of output digits is less than 6, 0 is used to make up 6 digits. The output of this row is '001. 24'. It can be seen that the decimal point also occupies one place. Tags similar to 0 include-and +. -Indicates the left alignment, and + indicates that the plus sign is also placed before the positive number. By default, the plus sign is not added. Finally, let's look at the most complex form:
'% (Name) s: % (score) 06.1f' % {'score': 9.5, 'name': 'newsim '}
In this form, only when the output content is dictionary (a python data type), the (name) and (score) in parentheses correspond to the keys in the subsequent key-value pairs. In the previous example, we can see that the order marked in the "format mark string" and the values in the "value group to be output" are one-to-one, ordered, one-to-one, and two-to-two. In this form, no value corresponding to each format mark is specified by the key in parentheses. The output of this line of code is: 'newsim: 808080 '.
 
Sometimes in the form of % 6.2f, 6 and 2 cannot be specified in advance and will be generated during the program running. How can we input this? Of course, % d cannot be used. % df or % d. % d % f. It can be in the form of % *. * f. Of course, the following "value group to be output" contains the two * values. For example, '% *. * F' % (6, 2, 2.345) is equivalent to' % 6.2f '% 2.345.
This is the most complex content this book has ever seen. However, if you cannot remember, or... the remaining full text>

Python and return use the % operator to format the output string

Return is the expression used to return the function value to call the function value.
If you do not use return to call g (a), the code is a statement rather than an expression. That is, g (x) in the Code does not represent any value.
For example, if "> g (1)" and g (a) has return 2, 2 is returned.

2:
A %. 1f floating point number accuracy 1 bit
B %. 2e real number (Science and Technology Development) Accuracy 2 bits
C % 04d integer 4-digit complement with 0
D % d integer

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.