Format method in 31 python: field width, precision and thousands of bits
Lesson 6 Field width, precision and thousand-bit delimiter (format method) # field width, precision and thousand-bit separator # 100000000000 allows a numeric value to be output in a width range of 2, if the value is less than 12 bits Fill in the blanks on the left with 4 bits print ("a: {num:2}" .format (num = 32)) # a:32print ("a: {num:4}" .format (num = 32)) # aVue 32 with two spaces including 32. These two spaces are four widths # field widths can be used to make a table # create tableprint ("{header1:10} {header2:6} {header3:0}" .format (header1= "name") in python. Header2= "age", header3=2) # name age 2 # explain the results output above # name followed by age (including age is 10 widths) age to 2 (age excluding 2) is 6 width 2 there are no spaces behind 0 width print ("{header1:10} {header2:2}" .format (header1= "name") Header2= "age") print ("{cell11:10} {cell12:2}" .format (cell11 = "Bill", cell12=43)) print ("{cell21:10} {cell22:4}" .format (cell21 = "Mike") Cell22=34)) name age Bill 43Mike 34 Note the number is the left filling space for the string is the right filling space from math import piprint ("float number: {pi:.3f}" .format (pi = pi)) # float number:3.142print ("float number: {pi:20.4f}" .format (pi = pi)) # float number: 3.14 explanation Explanation: contains a 20-bit # intercept string print ("{msg:.5}" .format (msg = "Hello World")) # Hello# explains the interception of the first five strings print ("{msg:.3}" .format (msg = "Hello World")) # Hel# thousand delimiter print ("One googol is {: } ".format (10 * * 10) # One googol is 10000000000 lesson 7 symbol, align and fill with 0 (fromat method) # symbol, Align and fill with 0 # 12 00000012from math import piprint ("{pi:012.3f}" .format (pi = pi)) # 00000003.142print ("{pi:#12.3f}" .format (pi = pi)) # 3.142 also indicates blank padding # (right-aligned) defaults to right-aligned print ("{pi:#12.3f}" .format (pi = pi)) #? ? 3.142print ("{pi:0=12.3f}" .format (pi =-pi)) #-0000003.142print ("{pi:?=12.3f}" .format (pi =-pi)) # -? 3.142print ("{pi:=12.3f}" .format (pi =-pi)) #-3.142