5. One of the modules detailed

Source: Internet
Author: User

1. Formatted output (1)--%:
TypeCode    required S, #获取传入对象的__str__方法的返回值, and format it to the specified position R, #获取传入对象的__repr__方法的返回值, and format it to the specified position C, #整数: Converts the number to its Unicode corresponding value , the 10 binary range is 0 <= i <= 1114111 (py27 only supports 0-255); character: Adds a character to the specified position O, #将整数转换成 a eight-binary  representation, and formats it to the specified position x, #将整数转换成十六进制表示, and format it to the specified position d, #将整数, floating-point number into a decimal representation, and format it to the specified position E, #将整数, floating-point number converted to scientific notation, and formatted to the specified position (lowercase e) e, #将整数, floating-point number converted to scientific notation, and formats it to the specified position (capital E) f,# converts integers, floating-point numbers to floating-point number representations, and formats them to a specified position (the default is 6 digits after the decimal point) F, #同上g, #自动调整将整数, floating-point numbers are converted to floating-point or scientific notation (more than 6 digits in scientific notation), and format it to the specified location (if the scientific count is E;) G, #自动调整将整数, floating-point numbers are converted to floating-point or scientific notation (more than 6 digits in scientific notation), and formatted to the specified position (if the scientific count is e;)%, #当字符串中存在格式化标志时, need Percent percent denotes a semicolon

This can be used in print, for example, as follows:

Print ("%s"% "Xiaoming") xiaomingprint ("%d"%1324) 1324

If you only use placeholders in print and you want to print "%", you need to do this:

Print ("%s%"% ' xiaoming ') Xiaomin%

If you want to output the value of the dictionary, you can do it with the following:

Print ('% (name) s  % (score) 06.1f '%{' score ': 9.5, ' name ': ' Newsim '})    #06.1f means: 0 means fill bit, 6 means this floating point is six bits (plus decimal point) , 1 means to keep a decimal, in general, 9.5 to keep a decimal place, the default is reserved six bits, not enough six bits from the front to add 0newsim  0009.5

2. Formatted output (2)--format
[[Fill]align] [Sign] [#] [0] [Width] [,] [. Precision] [Type]fill # "optional" whitespace filled characters align# "optional" alignment (with width use) <    #内容左对齐 >    #内容右对齐 (default) =   #内容右对齐, place symbols on the left side of the fill character, And only valid for numeric types. Even: symbol + filler + Number ^    #内容居中sign # "optional" with unsigned number +    #正号加正, minus plus negative;-    #正号不变, minus plus negative; space  #正号空格, minus plus minus; #    # "optional" for binary, octal , Hex, if you add #, it will show 0b/0o/0x, otherwise not display,   # "optional" to add a delimiter for the number, such as: 1,000,000width        # "optional" format bit of the width. Precision  # "optional" Decimal digit reserved Precision type         # "optional" format type parameter s passed in "string type", format string type data blank, unspecified type, default is None, with s passed parameter B of "integer type", #将10进制整数自动转换成2进制表示然后格式化c, # Automatically converts 10 binary integers to their corresponding Unicode characters D, #十进制整数o, #将10进制整数自动转换成8进制表示然后格式化; x, #将10进制整数自动转换成16进制表示然后格式化 (lowercase x) x,# Converts a 10-decimal integer into a 16-binary representation and then formats (uppercase X) The argument E that passes in the float or decimal type, #转换为科学计数法 (lowercase e), then formatted, E, #转换为科学计数法 (uppercase E), and then formatted; f,# Converted to floating point (6 digits after the default decimal point), then formatted, F, #转换为浮点型 (6 bits left after the default decimal point), then formatted, G, #自动在e和f中切换G, #自动在E和F中切换%, #显示百分比 (the default is 6 digits after the decimal point)  

Here are a few common patterns:

Pattern One: Normal placeholder, this is the same as print, but it's a little different.

Print ("{: S}{:s}". Format (' xiaoming ', ' Xiaohong ')) Xiaomingxiaohongprint ("{:d},{:s}". Format (12345, ' Xiaohong ') 12345,xiaohongprint ("{: 06.2f}". Format (0.234253)) 000.23

  

Pattern Two: Name placeholder, same as print

Print ("{name} {age}". Format (name= ' Alex ', age=23)) Alex 23 can also be written as follows, but this specifies the type of the value print ("{name:s} {age:d}". Format (name= ' Alex ', age=23)) Alex 23

Mode three: Print percent, directly after the placeholder to add a%

Print ("{:. 2%}". Format (0.234253)) 23.43%

Mode four: auto-fill function

"{:* ^20s}". Format (' Alex ')                       #居中显示alex, rest with * fill "{:* <20s}". Format (' Alex ')                       #右对齐显示alex and the rest are filled with ' {:* >20s} '. Format (' Alex ')                       #左对齐显示alex, rest with * Fill print ("{:* ^20s}". Format (' Alex ')  ) ********alex********print ("{:* >20s } ". Format (' Alex ')  ) ****************alexprint (" {:* <20s} ". Format (' Alex ')  ) alex****************

Pattern 6: Placeholder with numbers, note that the numbers here can be repeated but must start from 0

Print ("{0} {0} {1}". Format (' xiaoming ', ' Xiaohong '))    #0代表format中第一个, 1 for the second one, one analogy xiaoming xiaoming Xiaohong

Pattern 7: Only curly Braces

Print (' {} {} {} '. Format (' Zhangshan ', ' Lisi ', ' Wangwu ')     #如果只有大括号, then match the content in format from the previous Zhangshan Lisi Wangwu

 

Attached 1: progress bar (yum style), need to run in terminal to show, do not know what Ghost
Start = 0total =                                               #需要接受的数据的总数 while   start < int:    start + = 1                                          #1可以替换为每次接收数据的大小    i =                                              Total number of # to be printed    c = Int ((Start/int (total)) *                  #当前需要打印的 # Number of    P = float ((start/int (                     total))) #当前传输完毕的数据百分比 C10/>j = "#" * C    sys.stdout.write ("[{: <70s}]{:.0%}\r". Format (j,p))    Time.sleep (0.1)

Attachment 2: Exercises
TPL = "I am {}, age {}, {}". Format ("Seven", ' Alex ') TPL = "I am {}, age {}, {}". Format (*["Seven", "Alex"]) TPL = "I AM {0}, age {1}, really {0} ". Format (" seven ", +) TPL =" I am {0}, age {1}, really {0} ". Format (*[" seven ", +]) TPL =" I am {n " AME}, age {age}, really {name} '. Format (name= "Seven", age=18) TPL = "I am {name}, age {age}, really {name}". Format (**{"name") : "Seven", "age": +) TPL = "I am {0[0]}, age {0[1]}, really {0[2]}". Format ([1, 2, 3], [One, All,]) TPL = "I am {: s}, age {:d}, Money {: F} '. Format ("Seven", 88888.1) TPL = "I am {: s}, age {:d}". Format (*["seven", +]) TPL = "I am {name:s}, age {age:d} ". Format (name=" Seven ", age=18) TPL =" I am {name:s}, age {age:d} ". Format (**{" name ":" Seven "," Age ":) TPL =" Numbe RS: {: b},{:o},{:d},{:x},{:x}, {:%} ". Format ([2], [+]," 15.87623 "), TPL =" numbers: {: b},{:o},{:d},{:x},{:x}, {:%} " . Format (15.87623, 2) TPL = "numbers: {0:b},{0:o},{0:d},{0:x},{0:x}, {0:%}". Format (All) TPL = "Numbers: {n Um:b},{num:o},{num:d},{nUm:x},{num:x}, {num:%} ". Format (NUM=15) 

  

5. One of the modules detailed

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.