based learning python - format string

A, _ Percent string formatting%

1,% s replace all data types

name="I am %s my %s is %s"%("lucy","name","cc")
name1="I am %s my %s is %s"%("lucy","name",[1,2])
print(name)
print(name1)


结果:
I am lucy my name is cc
I am lucy my name is [1, 2]

2,% d replacement digital

name="I am %s my %s is %d"%("lucy","age",12)
name1="nana"
age=19
name1="my %s is %s"%(name1,age)
print(name)
print(name1)

结果:
I am lucy my age is 12
my nana is 19

3,% f float Print

= the nums " Percent% F " % 99.2345654345 # After six decimal Default 
Print (the nums) 

Results: 
Percent 99.234565

4, the median% .2s string interception

= the nums " word .3s% " % " abnfdhfjfj "  # interception string 3 
Print (the nums) 
Results:
word abn

5, print percentage% .2f

= the nums " % .2f %% " % 99.24356754334 # percentage figures, two reserved bits after the decimal 
Print (the nums) 
results:
 99.24%

6, various popular formats exercises percent sign%

 

 

 

Two, format- string formatting

 

Guess you like

Origin www.cnblogs.com/xucuiqing/p/11546329.html