python format

  

>>> "{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'
>>>

 

posted on 2017-10-09 18:31  尴尬而不失礼貌的微笑  阅读(301)  评论(0编辑  收藏  举报