Python output format and function format

Source: Internet
Author: User
Tags value of pi

This paper summarizes some simple and basic forms of output formatting and the basic usage form of the function Format function.

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

Use of basic methods:

 1 #!/usr/bin/python 2 #coding =utf-8 3 "4 You can specify the alignment of the desired length of the string: 5 < (default) left-justified 6 > right-aligned 7 ^ middle-aligned 8 = (for numbers only) after the decimal point 9 "' Print (' 1:\t|{ 0:&GT;10}, '. Format (' Wangyu ') print (' 2:\t|{ 0:4.2f} '. Format (1.1415926)) print (' 3:\t| ', Format (1.1415926, ' <10.2f ')) print (' 4:\t|{ 0:&LT;10},{1:&LT;15} '. Format (' Wangyu ', 1.1415926)) print (' 5:\t| User ID: {uid} last seen: {last_login} '. Format (uid= ' root ', Last_login = ' 5 Mar 2008 07:20 ')) 15 16 "The format indicator can contain a presentation type to control formatting 。 17 For example, a floating-point number can be formatted as a general format or represented by a power. ' B '-binary. The number is output as a base of 2. The ' C '-character. Converts an integer to the corresponding Unicode string before printing. The ' d '-decimal integer. The number is output as a base of 10. Eight ' o '-binary. The number is output as a base of 8. 16 ' x '-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. 26 '% '-percent. Multiply the value by 100 and then print in fixed-point (' F ') format, followed by a percent semicolon. "" "Print (' 6:\t|{ 0:B} '. Format (3))-Print (' 7:\t|{ 0:C} '. Format (3)) to 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)) #浮点数37 print (' 14:\t|{ 0:N} '. Format (3) #整数38 print (' 15:\t|{ 0:%} '. Format (3.75)) #输入形式的控制format41 a = Int (input (' A: '))--B = int (input (' B: ')) print (' 16:\t|%*.*f '% (A, B, 1.14 15926)) (' 17:\t|{ ARRAY[2]} '. Format (Array=range ()) "Print (' 18:\t|{ ATTR.__CLASS__} '. Format (attr=0)) print (' 19:\t|{ digit:*^ 10.5f} '. Format (DIGIT=1.0/3)) 48 49 "50 classes and types can define a __format__ () method to control how to format themselves. 51 it accepts a format indicator as a parameter: "__format__" (Self, Format_spec): si if Isinstance (Format_spec, Unicode): retur n Unicode (self), else:57 return str (self)

#!/usr/bin/python#coding=utf-8# use the ' {} ' placeholder print (' I\ ' m {},{} ') using the Str.format () function #. Format (' Hongten ', ' Welcome to my space! ') Print (' # ') #也可以使用 ' {0} ', ' {1} ' placeholder print (' {0},i\ ' m {1},my e-mail is {2} '. Format (' Hello ', ' hongten ', ' [email  Protected]) #可以改变占位符的位置print (' {1},i\ ' m {0},my e-mail is {2} '. Format (' Hongten ', ' Hello ', ' [email protected] ')) Print (' # ') #使用 placeholder print (' Hi,{name},{message} ' in the form of ' {name} '. Format (name = ' Tom ', message = ' How old is You? ')) Print (' # ') #混合使用 ' {0} ', ' {name} ' form print (' {0},i\ ' m {1},{message} '. Format (' Hello ', ' hongten ', message = ' This is a test message! ')) Print (' # ') #下面进行格式控制import mathprint (' The value of PI is approximately {}. '). Format (Math.PI)) print (' The value of pi is approximately {!r} '. Format (Math.PI)) print (' The value of pi is approximately {0:.3f} '. Format (MATH.PI)) Table = {' Sjoerd ': 4127, ' Jack ': 4098, ' dcab ': 7678}for name, phone in Table.items (): Print (' {0:10} ==& Gt {1:10d} '. Format (name, phone)) Table = {' Sjoerd ': 4127, ' Jack ': 4098, ' dcab ': 8637678}print (' Jack: {0[jack]:d}; Sjoerd: {0[sjoerd]:d}; ' Dcab: {0[dcab]:d} '. Format (table)

Original: http://www.cnblogs.com/RukawaKaede/p/6069977.html

Python output format and function format

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.