Python format string)

Source: Internet
Author: User
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. The value corresponding to each format mark is specified by the key in the 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 it or do not want to be so patient, you can replace it with % s, or use multiple "+" to construct similar output strings. Here % is really a bit of division, no wonder the designer will choose to use %.

Like the sprintf function in C, you can use "%" to format the string.

Table 3.1. String formatting code

Format description
% Percent mark
% C and its ASCII code
% S string
% D signed integer (decimal)
% U unsigned integer (decimal)
% O unsigned integer (octal)
% X unsigned integer (hexadecimal)
% X unsigned integer (hexadecimal uppercase)
% E floating point number (Scientific Notation)
% E floating point number (Scientific notation, replaced by e)
% F floating point number (decimal point)
% G floating point number (% e or % f based on the value)
% G floating point number (similar to % g)
% P pointer (memory address for printing value in hexadecimal format)
% N stores the number of output characters in the next variable in the parameter list

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.