码迷,mamicode.com
首页 > 其他好文 > 详细

字符串格式化

时间:2020-01-27 10:56:13      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:数字   pre   name   code   obb   span   接收   color   字符串   

1、打印浮点数%f

tp1="percent %.2f"%99.976234444444444444
print(tp1)

tp1="percent %.3f"%99.976234444444444444
print(tp1)

>>>

percent 99.98
percent 99.976

2、打印字符串%s,

      #字符串可接收数字,可接收浮点数

msg1 = my name is %s, my hobby is %s   %(fu,pingpang)
msg2 = his name is %s   %zheng
print(msg1)
print(msg2)

>>>

my name is fu, my hobby is pingpang
his name is zheng

3、打印数字型%d

msg1 = my name is %s, my age is %d   %(fu,20)
print(msg1)

>>>my name is fu, my age is 20

4打印百分号%%

tpl= percent %.2f %%     % 99.976234444
print(tpl)

>>>percent 99.98 %

字符串格式化

标签:数字   pre   name   code   obb   span   接收   color   字符串   

原文地址:https://www.cnblogs.com/forhowcar/p/12235463.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!