Python中的%占位符%s,%d%和%f等

Python中用%代表格式符,或者叫占位符
表示格式化操作,将其转化成相应的数据类型

例如:
%s占位:字符串,采用str()的显示。并将数据转化为字符串形式

string="hello"  
print(string=%s" % string)# %s打印时被替换为hello  
# output: string=hello  

注:每种占位符代表不同的数据类型

其他占位符:

占位符 用途
%r 字符串,采用repr()的显示
%c 单个字符
%d 十进整数
%i 十进整数,同%d
%o 八进整数
%x 十六进整数
%e 指数,基底为e
%E 指数,基底为E
%f 浮点型
%F 浮点型,同%f
%g 指数e或浮点型,根据显示长度决定
%G 指数E或浮点型,根据显示长度决定

猜你喜欢

转载自blog.csdn.net/coberup/article/details/82856910