python输出字符串的格式控制_Python字符串操作符(%s,%d,%u...)与输出格式控制(*-+#0...)...

#字符串操作符与格式控制

'''''

----------------------------

%c  ASC2或者长度为1的字符串

%s  __str__()

%r  __repr__()

%d %i  有符号十进制

%u  无符号十进制

%o  无符号八进制

%x %X  无符号十六进制

%e %E  科学计数法

%f %F  float(小数部分自动截断)

%g %G  ==%e+%f ==%E+%F

%%  print '%'

-----------------------------

*       定义宽度或小数点精度

-       左对齐

+       在正数前面显示加号

在正数前面显示空格

#       八进制显示0,十六进制显示0X或者0x

0       数字前面填充0而不是空格

(var)   映射变量,字典参数

m.n     最小总宽度,小数点后的位数

-----------------------------

'''

'''''

#test 0x00

print '%x' % 108, '%X' % 108

'6c 6C'

print '%#X' % 108

'0X6C'

#test f e E .2 g G

print '%.2f' % 1.2345678

'1.23'

print '%E' % 1234.567890

'1.234568E+03'

print '%g' % 1234.567890

'1234.57'

#test +d i s o

print '%+d' % 4

'+4'

print '%+d' % -4

'+4'

print '%d%%' % 100

'100%'

print '%s %s' % ('hello', 'world')

'hello world'

print '%#o/hex' % 123

'0173/hex'

print '%02d/%02d/%02d' % (2, 15, 67)

'02/15/67'

#test dict

print 'There are %(key1)d %(key2)s Quotation Symbals' % \

{'key2': 'Python', 'key1': 3}

'There are 3 Python Quotation Symbals'

'''

'''''

一种针对dict更方便的表示方法——使用${}

from string import Template

s = Template('There are ${key1} ${key2} Quotations Symbols')

print s.substitute(key2='Python', key1=3)

'There are 3 Python Quotation Symbals'

print s.substitute(key2='Python')

'KeyError: 'key1''

print s.safe_substitute(key2='Python')

'There are ${key1} Python Quotations Symbols'

'''

'''''

#u/U and r/R

ur'Hello\nWorld!'

'''

引自http://blog.csdn.net/cd_xuyue/article/details/46583113

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值