字符串格式化

Posted forhowcar

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串格式化相关的知识,希望对你有一定的参考价值。

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 %

以上是关于字符串格式化的主要内容,如果未能解决你的问题,请参考以下文章