Python formatted character%s%d%f

Source: Internet
Author: User

Format description
Percent percentile sign #就是输出一个%
%c character 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 (16 uppercase characters)
%e floating-point numbers (scientific notation)
%E floating point number (scientific notation, E instead of e)
%f floating point number (with decimal point symbol)
%g floating-point numbers (%e or%f depending on the size of the value)
%G floating-point number (similar to%G)
%p pointer (memory address with hexadecimal print value)
%n the number of stored output characters into the next variable in the parameter list

The% format character can also be used in dictionaries, and the output can be formatted by using% (name) to refer to the elements in the dictionary.

The minus sign indicates that the number should be left-aligned, and "0" tells Python to fill the number with a leading 0, plus the number always displays its positive and negative (+,-) symbol, even if the number is positive.

You can specify the minimum field width, for example: "%5d"% 2. A period character can also be used to specify additional precision, such as "%.3d"% 3.

# example: Numeric formatting
nyear = 2018
Nmonth = 8
Nday = 18
# formatted date%02d number to two-bit integer vacancy fill 0
print '%04d-%02d-%02d '% (nyear,nmonth,nday)
>> 2018-08-18 # Output results

Fvalue = 8.123
print '%06.2f '%fvalue # reserved 2-bit decimal floating-point with a width of 6
>> 008.12 # Output

print '%d '%10 # output decimal
>> 10
print '%o '%10 # output octal
>> 12
print '%02x '%10 # output two-bit hex, letter lowercase blank complement 0
>> 0a
print '%04x '%10 # output four-bit hex, letter capital Vacancy 0
>> 000A
print '%.2e '%1.2888 # Output floating-point type with scientific notation reserved 2 decimal places
>> 1.29e+00

Formatting operator Auxiliary directives
Symbolic effect
* Define width or decimal point accuracy
-Used for left alignment
+ Show plus sign (+) in front of positive number
<sp> show spaces in front of positive numbers
# 0 (' 0 ') is displayed in front of the octal number, preceded by ' 0x ' or ' 0X ' in hexadecimal (depending on
Using ' x ' or ' x ')
0 The number shown is preceded by ' 0 ' instead of the default space
% ' percent ' output a single '% '
(VAR) mapping variable (dictionary parameter)
M.N m is the smallest total width displayed, and n is the number of digits after the decimal point (if available)

Python formatted character%s%d%f

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