The format method of the ''' string object uses the e.g. {food}) indicates the replacement target and the parameters to be inserted ''' template= '{0},{1} and {2}' print (template.format( 'spam' , 'ham' , 'eggs' )) #spam,ham and eggs template= '{motto},{pork} and {food}' print (template.format( motto = 'spam' , pork = 'ham' , food = 'eggs' )) #spam,ham and eggs template= '{motto},{0} and {food}' print(template.format('ham',motto='spam',food='eggs')) #spam,ham and eggs x='{motto},{0} and {food}'.format(42, motto=3.14, food=[1,2]) print(x) #3.14,42 and [1, 2] print(x.split('and')) #['3.14,42 ', ' [1, 2]'] print(x.replace('and', 'but under no circumstances')) #3.14,42 but under no circumstances [1, 2] #Format strings can specify object properties and dictionary keys, square brackets specify dictionary keys, and dots indicate object properties of an item referenced by a position or keyword import sys print ( 'my {1[spam]} runs {0. platform}' .format(sys, { 'spam' : 'laptop' })) #0 means sys, 0.platform is an attribute of sys, 1 means dictionary: {'spam':'laptop'}[spam] my laptop runs win32 print ( 'my {config[spam]} runs {sys.platform}' .format( sys =sys, config ={ 'spam' : 'laptop' })) #my laptop runs win32 #The format string can be Specifying a list offset has performed indexing, however, only a single positive offset is valid in the format string syntax somelist= list ( 'SPAM') print (somelist) print('first={0[0]}, third={0[2]}'.format(somelist)) #first=S, third=A print('first={0}, last={1}'.format(somelist[0], somelist[-1])) #first=S, last=M parts=somelist[0], somelist[-1], somelist[1:3] print('first={0}, last={1}, middle={2}'.format(*parts)) #first=S, last=M, middle=['P', 'A']
''' Add specific formatting Use a colon after the identifier of the replacement target, followed by a formatting declaration that can specify the field size, alignment and a specific type of encoding Structure: {fieldname!conversionflag:formatspec} fieldname is the specified parameter A number or keyword followed by an optional ".name" or "[index]" component reference conversionflag can be r, s, or a respectively a call to the repr, str or ascii built-in function on the value formatspec Specifies how to represent the value, including details such as field width, alignment, zero padding, and decimal point precision, and ends with an optional data type encoding formatspec form: [[fill]align][sign][#][0] [width][.precision][typecode] align may be <, >, =, ^, indicating left alignment, right alignment, supplementary or center alignment after a mark character, formatspec also contains nested, only with { } format string, which dynamically obtains the value from the argument list (similar to * in format expressions). The format method also allows a "b" type encoding to display integers in binary format (equivalent to using bin built-in function) allows a "%" type encoding to display percentages ''' import sys print ( '...{0:10}...{1:10}...' .format( 'spam', 123.4567)) #10 means width 10 ...spam ... 123.4567... print ( '...{0:<10}...{1:<10}...' .format( 'spam' , 123.4567 )) #<10 means width 10, left justified...spam...123.4567... print ( '...{0:>10}...{1:>10}...' .format ( 'spam' , 123.4567 )) #>10 means width 10, right justified... spam... 123.4567... print ( '...{0:^10}...{1:^10}. ..' .format( 'spam' , 123.4567 )) #>10 means width 10, center alignment... spam ... 123.4567 ... print ( '...{0.platform:>10}={1 [item]:<10}...'.format(sys, dict ( item = 'laptop' ))) #... win32=laptop ... print ( '{0:e},{1:.3e},{1:g}' .format( 3.14159 , 3.14159 , 3.14159 )) #The number of decimal places and data type after the colon 3.141590e+00,3.142e+00,3.14159 print ( '{0:f},{1:.2f},{2:06.2f}' .format( 3.14159 , 3.14159 , 3.14159 )) #3.141590,3.14,003.14 print ( '{0:x},{1:o},{2:b}' .format( 255 , 255 , 255 )) #hexadecimal , octal , binary ff, 377, 11111111 print ( '{0:.2f}' .format( 1 / 3.0 )) #0.33 print ( '%.2f' %( 1 / 3.0 )) #0.33 print ( '{0:.{1}f}' . format( 1 / 3.0 , 4 )) #0.3333 print ( '%.*f' %( 4 , 1 / 3.0 )) #0.3333 # A cleaner alternative to string formatting methods print ( '{0: .2f}' .format( 1.2345 ) ) #1.23 print ( format ( 1.2345 , '.2f' )) #1.23 print ( '%.2f' % 1.2345 ) #1.23 #thousands division
print('{0:d}'.format(9999999999)) #9999999999 print('{0:,d}'.format(9999999999)) #9,999,999,999 print('{0:,.2f}'.format(9999999.999)) #10,000,000.00
#binary format
print('{0:b}'.format(255)) #11111111
#Number replacement value input optional
print('the {} side {} {}'.format('a','b','c'))