码迷,mamicode.com
首页 > 编程语言 > 详细

python format

时间:2017-10-09 19:37:19      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:module   oat   格式化字符串   form   关键字   格式化   rac   com   格式   

  

>>> "{0} love {1}.{2}".format("I","You","com")
‘I love You.com‘
>>>

>>> "{a} love {b}.{c}".format("I","You","com")
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
"{a} love {b}.{c}".format("I","You","com")
KeyError: ‘a‘
>>> "{a} love {b}.{c}".format(a="I",b="You",c="com")
‘I love You.com‘  //需要a为关键字
>>>

>>> print("\ta")  //转义
a
>>> print("\\")
\
>>> "{{0}}".format("不打印")
‘{0}‘

"{0.1f}{1}".format(27.658,‘GB‘)
AttributeError: ‘float‘ object has no attribute ‘1f‘
>>> "{0:.1f}{1}".format(27.658,‘GB‘)  //四舍五入
‘27.7GB‘
>>>

>>> ‘%c‘ % 97  //a的ascii码为97
‘a‘
>>>

>>> "%c" "%c" "%c" % (97,98,99) //格式化ascii码
‘abc‘
>>> "%c %c %c" % (97,98,99)  
‘a b c‘
>>>

>>> "%d + %d = %d" % (4,5,4+5)  //格式化数字
‘4 + 5 = 9‘
>>> "%s" % " i love you" //格式化字符串
‘ i love you‘
>>>

 

python format

标签:module   oat   格式化字符串   form   关键字   格式化   rac   com   格式   

原文地址:http://www.cnblogs.com/xiaohouzai/p/7642394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!