When formatting a string, Python uses a string as a template. There are formatting characters in the template that reserve locations for real values and describe the format in which real values should be rendered. Python uses a tuple to pass multiple values to the template, each of which corresponds to a format character.
There are two ways to format a python string
Way one:% percent sign way
1. Syntax:
%[(name)][flags][width]. [Precision]typecode
2. Common formatting:
(1) Value of flyer (%s can receive any type of value)
Tpl="I am%s""xixi"print (TPL)
Output:
I am Xixi
2
(2) Pass multiple values
Tpl='I am%s My hobby is%s' % ('xixi','XI ' )print(TPL)
Output:
I am xixi my hobby is XI
(3)%d can only transmit numbers
Name='xixi'age =77TPL='I am%s My hobby is%s' % (name,age)print(TPL)
Variable name= ' Xixi ', age=77 add a name by%s,% add the output:
I am xixi my hobby is 77
(4) Splice string% () s% () d dictionary by key
" I am% (name) s age percent (age) D" % {"name""Alex" " Age ":"print(TPL)
By passing the value, Alex and 18 are printed as the value of the value% (name) S,value (age) D of the key value :
I am Alex age 18
(5) Print floating point percent%.2f
" percent%.2f " % 99.976234444444444444print(TPL)
Print two bits (rounded) output after the decimal point :
Percent 99.98
(6) percent print%
' percent%.2f ' % 99.976234444444444444print(TPL)
Print the two-bit percent output after the decimal point :
Percent 99.98
(6) percent print%
' percent%.2f ' % 99.976234444444444444print(TPL)
Print the two-bit percent output after the decimal point :
Percent 99.98%
(7) Add delimiter to String sep= '
Print ('root','x','0',' 0', sep=':')
Output:
root:x:0:0
Mode two: Format mode
1. Syntax
It replaces% with {} and:.
Map sample
2. Common formatting:
(1) Format normal pass value (must be one by one corresponding relationship)
" I am {}, age {} " . Format ("seven")print(TPL)
Output:
I am seven, age 18
(2) Value by index method
" I am {2}, age {1}, {0} " . Format ("seven", +,'Alex')Print (TPL)
Output:
I am Alex, age, seven
(3) According to the Ganso Way value:
" I am {0}, age {0} " . Format ("seven")print(TPL)
Output:
I am seven, age seven
(4) Pass the value as a dictionary:
Mode 1:
" I am {name}, age {age}, really {name} ". Format (name="seven", age=18)print(TPL)
Print:
I am seven, age, really seven
Method 2:** the first key value of the dictionary to be taken out to convert to a form pass value
" I am {name}, age {age}, really {name} ". Format (**{"name""seven"" Age ":")print(TPL)
Print:
I am seven, age, really seven
(5) Pass the value by the list way:
" I an {0[0]},age{0[1]},really {0[2]} ". Format ([+)]print(TPL)
printed:0[0] represents the 1 of the first small list that takes the first element of a tuple's ancestor
I an 1,age2,really 3
(6) The value is passed as a string:: S is similar to%s,:D represents the int type,: F stands for floating-point type
" I am {: s}, age {:d},money {: f} ". Format ("seven", 77,777.7)print(TPL)
Print:
I am seven, age 77,money 777.700000
(7) * Go through the elements in the list and pass the values as a list
" I am {: s}, age {:d} ". Format (*["seven", +])print(TPL)
Print:
I am seven, age 18
4.python string Formatting