Python string Formatting code

Source: Internet
Author: User
Format Summary:

Format description format description
%% percent sign% (extra% is escape effect) /td>
% c character and its ASCII code %s string
%d signed integer (decimal) %u unsigned integer (decimal)
%o unsigned integer (octal)
%x unsigned integer (hex) % x unsigned integer (16 uppercase characters)
% E floating-point numbers (scientific notation) % e floating-point numbers (scientific notation, E in lieu of e)
%f floating point number (with decimal point symbol)
%g floating-point numbers (automatically select%e or%f depending on the value) % g floating-point numbers (similar to%g, automatically select%e or%f based on different values)
%p pointer (memory address with hexadecimal value printed) %n store output characters in the next variable in the parameter list

However, because in Python, everything can be converted to a string type, so there is no special need to fully use '%s ' to mark it all.
For example: '%s%s '% (1, 2.3, [' One ', ' one ', ' three ')
Its output is ' 1 2.3 [' One ', ' one ', ' one ', ' three '] ', which is output according to the mark on the left of the%.
Although the first and second values are not of type string, they are not a problem.
[In this process, when the computer discovers that the first value is not%s, the function of the integer number is called First, the first value is 1 to the string type, and then the STR () function is called to output. ]

It's all about the simplest form of formatting tags, to look at a bit more complicated:
Set the 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 long, where the decimal is 2 bits.

There are more complications:
PLACEHOLDER:
'%06.2f '% 1.235
In front of 6 more than a 0, indicating that if the output of less than 6 bits of 0 to complement 6 bits.
The output of this line is ' 001.24 ', and you can see that decimals also occupy one bit.

Similar to the 0 here, there is a-, +. Where,-Indicates left alignment, + indicates that the plus sign is preceded by a positive number and is not added by default.

Instance:
%ns: If the string variable s is less than n bits, it will output a few spaces before outputting str1
%-ns: In contrast to the above, the output variable s, insufficient and then with a space in the back.

Other:
Dictionary formatted output:
'% (name) s:% (score) 06.1f '%{' score ': 9.5, ' name ': ' Newsim '}
This form is only used when the content to be output is dictionary. The parentheses (name) and (score) 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 the program, then how to input it, of course, can not be used%%D.%DF or%d.%d%f.
It can be in the form of%*.*f, of course, in the later "value group to output" contains 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, you can completely replace with%s, or use multiple "+" to construct a similar output string.

  • 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.