Python Print formatted output

Source: Internet
Author: User

First, Quick Search Handbook

1. String Formatting code:

format Description
%% Percent Sign
%c Characters and their 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 numbers (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 numbers (similar to%g)
%p Pointer (memory address of the value printed in hexadecimal)
%n Stores the number of output characters into the next variable in the parameter list

2. You can specify the alignment of the string for the desired length:< (default) align Left> Right Alignment^ Middle Alignment= (for numbers only) after the decimal point
3. Formatting Indicators' B '-binary. The number is output as a base of 2. ' C '-character. Converts an integer to the corresponding Unicode string before printing. ' d '-decimal integer. The number is output as a base of 10. ' o '-eight binary. The number is output as a base of 8. ' x '-16 binary. The number is output in 16, and the number of digits above 9 is in lowercase letters. The ' e '-power symbol. Print numbers in scientific notation. Use ' e ' to indicate power. ' G '-general format. The value is output in fixed-point format. When the number is particularly large, it is printed in a power form. ' n '-number. When the value is an integer and ' d ', the values are the same as the ' G ' when they are floating-point numbers. The difference is that it inserts a numeric delimiter according to the locale. '% '-percent. Multiply the value by 100 and then print in fixed-point (' F ') format, followed by a percent semicolon.
Second, the case

# Prints the string print ("His name was%s"% ("Aviad")) #His name is aviad# prints integer print ("He is%d years old"%) #He was years old&  Lt;pre name= "code" class= "Python" >print ("He is%d years old"%) #He is a years old# print floating-point printing ("his height is%f M "% (1.83)) #His height is 1.83 m# print floating-point number (specify reserved decimal digits) print (" His height is%.2f m "% (1.83)) #His height is 1.83 m" Print "Hi s height is ", format (1.83, '. 2f ')," M "# Specifies placeholder width print (" name:%10s age:%8d height:%8.2f "% (" Aviad ", 25,1.83)) #Name: Aviad Ag E:25 height:1.83# Specifies the placeholder width (left-aligned) print ("name:%-10s age:%-8d height:%-8.2f"% ("Aviad", 25,1.83)) #Name: Aviad age:25 Height: 1.83# specifies a placeholder (with 0 or a space as a placeholder) print ("name:%-10s age:%08d height:%08.2f"% ("Aviad", 25,1.83)) #Name: Aviad age:00000025 Height : 00001.83# calls the Format function, format (value, ' format ') print (' test:{0:10f} '. Format (Math.PI)) #test: 3.141593# If the output bits are less than 10, the default is right-aligned. If the output bits are greater than the width, then output format (0.0015, '. 2e ') as the actual number of digits #1.50e-03# Format indicator print ' 6:\t| {0:b} '. Format (3) print ' 7:\t| {0:c} '. Format (3) print ' 8:\t| {0:d} '. Format (3) print ' 9:\t| {0:o} '. Format (3) print ' 10:\t| {0:x} '. Format (3) print ' 11:\t| {0:e} '. Format (3.75) print ' 12:\t| {0:g} '. Format (3.75) print ' 13:\t| {0:n} '. Format (3.75) #浮点数print ' 14:\t| {0:n} '. Format (3) #整数print ' 15:\t| {0:%} '. Format (3.75)

Iii. Summary

1. Two styles of output

Python has a total of two formatting output syntaxes

One is similar to the C-language printf approach, called formatting Expression

[Python]View Plaincopyprint?
  1. >>>Print '%s%d-%d ' % (' Hello ', 7, 1)  
  2. ' Hello 7-1 '   
  3. >>> print ("He is%d years old"% (25))
  4. ' He's years old '

Basically the previous Text format control,% (,,) in the output content respectively.

The other is a C #-like way, called string formatting method Calls

[Python]View Plaincopyprint?
  1. >>>Print ' {0} {1}:{2} '. Format (' Hello ', ' 1 ', ' 7 ')  
  2. ' hello 1:7 ' &NBSP;&NBSP;
  3. >>>print ' test:{0:10f} '. Format (MATH.PI)
    ' test:3.141593 '

Basically, the preceding text Format control,. Format (,,) represents the output separately. Number (0, 1, ...) Represents the element inside the format (), so you can use the. method to invoke the element;

The parameters of the string are represented by {NUM}, 0, representing the first parameter, 1, representing the second parameter, and then sequentially sliding scale;

Use: To specify the actions required to represent the element, such as:. 3 decimal Three bits,: 8 for 8 character space, etc.;

Use of the format () function: It replaces% with {} and:.

the difference between the string formatting% in 2.Python and format
A= ' is%s '% (1) b= ' is {0} '. Format (1)

String formatting common% is the printf in C

Format is the Python string's own method and is recommended in format because it is more flexible

% () is equivalent to. Format (), which is the output in parentheses

The former uses C-style, the latter in C # style

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python print formatted output

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.