Python string format code _python

Source: Internet
Author: User
Tags python string format string format in python

Format Rollup:

percent% sign (extra % is an escape effect) td> string
Format description format description
%%    
% c character and its ASCII code %s
%d signed integer (decimal) %u unsigned integer (decimal)
%o unsigned integer (octal)    
%x unsigned integer (hexadecimal) % X unsigned integer (16 in uppercase characters)
% E floating-point number (scientific notation) % e floating-point number (scientific notation, substituting E for e)
%f floating-point number (with the decimal point symbol)    
%g floating-point number (automatically select%e or%f based on value) % G floating point number (similar to%g, automatically select%e or%f depending on value)
%p pointer (memory address that prints values in hexadecimal) %n the number of stored output characters is placed in the next variable in the argument list

But since in Python everything can be converted to string type, if there is no special requirement, it can all be marked with '%s '.
For example: '%s%s '% (1, 2.3, [' One ', ' two ', ' three '])
Its output is ' 1 2.3 [' One ', ' two ', ' three '] ', which is printed according to the mark on the left side of the%.
Although the first and second values are not string types, there is no problem.
[In this process, when the computer finds that the first value is not%s, it invokes the integer function, turns the first value, 1, to a string type, and then calls the STR () function to output. ]

It's just the simplest form of a format tag to look at something more complicated:
Set Output Length:
'%6.2f '% 1.235
In this form, a decimal-like 6.2 appears in front of F, which means that the total output is 6 characters, with a decimal number of 2 bits.

There are more complex:
Placeholder characters:
'%06.2f '% 1.235
A 0 more in front of 6 means that if the output is less than 6 digits, 0 is used to complement 6 digits.
The output of this line is ' 001.24 ' and you can see that the decimal is also occupied by one digit.

Similar to the 0 mark here--, +. Where,--represents the left alignment, + indicates that a positive number is preceded by the + sign, which is not added by default.

Instance:
%ns: If the string variable s is less than n bits, it will output several spaces before outputting str1
%-ns: In contrast to the above, the first output variable s, insufficient and then padded with blanks.

Other:
Dictionary format output:
'% (name) s:% (score) 06.1f '%{' score ': 9.5, ' name ': ' Newsim '}
This form is used only when the content to be exported is dictionary. The (name) and (score) in parentheses correspond to the keys in the following key-value pairs.

Sometimes in the form of%6.2f, 6 and 2 can not be specified in advance, will be generated in the process of running the program, how to input it, of course, can not use%%D.%DF or%d.%d%f. The
can be in the form of%*.*f, of course, in the following "set of values to output" containing the values of those two *.
For example: '%*.*f '% (6, 2, 2.345) is equivalent to '%6.2f '% 2.345.
However, if you do not remember, or do not want to be so impatient, all can be replaced with%s, or with multiple "+" to construct a similar output string.

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.