字符串格式化

%d十进制整数
%i 十进制整数
%u  无符号十进制#用法?
%o八进制整数
%x%X

小写/大写十六进制整数

%e%E       科学计数法(e/E决定e的大小写)
%f%F浮点数
%g%G  在科学计数法和浮点数中择优
%c单个字符
%s字符串
%r适用于原样输出
%%

输出中有%时,%%输出%

输出中原本无格式化,%输出%

 

    规范的语法解释字符串格式化:http://www.cnblogs.com/wilber2013/p/4641616.html

 

 

 

 

 

 

 

 

 

 

 

 

————————————————————————————————字符串格式化辅助符号———————————————————————————————————————————

 

*     定义宽度或者小数点精度
-  左对齐
+正数显示+号
0左侧空格用0替代
#

八进制前加0

十六进制前加0x

m.n

m:长度

n:精度

(?定义小数点精度时:*wants int)

 

 

 

 

 

 

 

 

# -*- coding:utf-8 -*-

a = 12
print "test %d" % - 10
print "test %i" % 10
print "test %o" % -8
print "test %u" % - 10 #unknown
print "test %x" % 15
print "test %X" % 15
print "test %e" % 10
print "test %E" % 10
print "test %f" % 12.2
print "test %F" % 12.2
print "test %g%G" % (12.2, 12.2)
print "test %c" % 'c'
print "test %r" % 'string'
print "test %s" % 'string'
print "test % %%"
print "test %"
print 'growth rate: %d %%' % 7

#
d = 100000
i = -d
u = d
f = 10.123456
s = 'this is a string'
o = 8
x = 15
print "test %10d" %d # m.n的用法
print "test %-10d" %d # -
print "test %*d %*d" %(10, d, -10, d) # *的用法,注意括号内定义长度后加逗号
print "test %+d %+i %+u" %(d, i, u) # +的用法
print "test %0*d %0*d" %(10, d, -10, d) # 0和*组合,0在*前面
print "test %#o, %#x" %(o, x) # #的用法
print "test %5.4f" % f
print "test %0#*x" % (10, x) # 0,#, *合用

 

 

转载于:https://www.cnblogs.com/nopear/p/5766207.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值