字符串拼接:

%s表示可以传任意类型的值,%d表示只能传数字

test = "my name is %s,age %d" %("xyp",19)
print(test) 

结果:

或者:

test = "my name is %(name)s,age %(age)d" %{'name':'xyy','age':19}
print(test)  // 这种方式传的值只能是字典

结果:

%f后面接的是浮点数(小数)默认小数点后面是六位数,不够的用0补

test = "num is %f" %99.97682
print(test) 

结果:

%.2f表示小数点后面的数保留两位并会自动进行四舍五入,可以是.3.4.5

test = "num is %.2f" %99.97682
print(test) 

结果:

或者:

test = "num is %(num).2f" %{'num':99.97682}
print(test)

结果:

 

%%表示在数字后面加上一个%号

test = "percent is %.2f%%" %99.97682
print(test) 

结果:

 

posted on 2017-06-26 15:42  花豆豆  阅读(112)  评论(0编辑  收藏  举报