There are two ways to format a python string: percent-percent, format-method
#%[(name)][flags][width]. [Precision]typecode
# %s followed by direct add% after if S is the same as any type, D is followed by the integral type
of the gym
Tel ="I am%s Hello World"%'Lucky'Print(tel)Print("I am%s My age is%d"%('Lucky', 20))Print('I am% (name) s my age are% (age) D'%{'name':'Lucky',' Age': 20})#string InterceptionPrint('I am%.3s'%'Abcderg')#only three-bit characters#floating point numberPrint('I am precent%.2f'%1.234)Print('I am% (PP). 2f'%{'pp': 1.2345})#keep two decimal placesA ='%.2f'%12.3Print(a)#TPL = "I am%.2f percent"% {"pp": 123.425556,}#print (TPL)Format method
TPL ="I am {}, age {}, {}". Format ("Seven", 18,'Alex') TPL="I am {}, age {}, {}". Format (*["Seven", 18,'Alex']) TPL="I am {0}, age {1}, really {0}". Format ("Seven", 18) TPL="I am {0}, age {1}, really {0}". Format (*["Seven", 18]) TPL="I am {name}, age {age}, really {name}". Format (name="Seven", age=18) TPL="I am {name}, age {age}, really {name}". Format (**{"name":"Seven"," Age": 18}) TPL="I am {0[0]}, age {0[1]}, really {0[2]}". format ([1, 2, 3], [11, 22, 33]) TPL="I am {: s}, age {:d}, Money {: F}". Format ("Seven", 18, 88888.1) TPL="I am {: s}, age {:d}". Format (*["Seven", 18]) 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": 18}) TPL="numbers: {: b},{:o},{:d},{:x},{:x}, {:%}". Format (15, 15, 15, 15, 15, 15.87623, 2) TPL="numbers: {: b},{:o},{:d},{:x},{:x}, {:%}". Format (15, 15, 15, 15, 15, 15.87623, 2) TPL="numbers: {0:b},{0:o},{0:d},{0:x},{0:x}, {0:%}". Format (15) TPL="numbers: {num:b},{num:o},{num:d},{num:x},{num:x}, {num:%}". Format (NUM=15)
Python string formatting