1、格式符

例如:

a = 'test'

print 'it is a %s' %(a)

打印的结果就是 it is a test

2、单独看%,是一个运算符号,求余数。

例如:

求模运算,相当于mod,也就是计算除法的余数,比如5%2就得到1。

扩展资料:

python中%常用的操作有%s,%d,%r等

%s,%r,%d分别表示字符串以str(),rper(),以及十进制整数表示,%f表示结果为浮点型。

1、%f 浮点型

import math    

%a.bf,a表示浮点数的打印长度,b表示浮点数小数点后面的精度 ,%f时表示原值,默认是小数点后5位数 。   

print "PI=%f" % math.pi

output: PI=3.141593

只是%9f时,表示打印长度9位数,小数点也占一位,不够左侧补空格

print "PI=%9f" % math.pi

output: PI=_3.141593

2、%d 整型

[python] view plain copy

num=14    

#%d打印时结果是14

3、%s 字符串

[python] view plain copy

string="hello"        

%s打印时结果是hello

更多推荐

python中%f是什么意思