Python formatted string instance summary, python string instance

Source: Internet
Author: User

Python formatted string instance summary, python string instance

The examples in this article summarize the methods for formatting strings in python and share them with you for your reference. The specific analysis is as follows:

The following example describes how to format a python string:

* Defines the width.
The Python code is as follows:

>>>'%*s' %(5,'some')' some'

-Left alignment
The Python code is as follows:

>>>'%-*s' %(5,'some')'some '

A 2-digit floating point decimal point with a minimum width of 6. If the number of digits is not enough, add a space before it.
The Python code is as follows:

>>>'%6.2f' %8.123' 8.12'

Dictionary format. The plus sign can be displayed before a positive number. If the number of digits is not enough, add 0 in front.
The Python code is as follows:

>>>'%(name)s = %(num)+06.2f' %{'name':'a','num':8.123}'a = +08.12'

Display zero ('0') before the octal number and '0x 'or '0x' before the hexadecimal number (depending on whether 'X' or 'X' is used ')
The Python code is as follows:

>>>'dec: %d/oct: %#o/hex: %#X' % (123,123,123)'dec: 123/oct: 0173/hex: 0X7B'

Scientific notation
The Python code is as follows:

>>>'%e' % 1234.567890'1.234568e+03'

I hope this article will help you with Python programming.


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 Format String

"{0: 02d}". format (4)

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.