标签:使用 io for ar sp on c python type
>>> reply = """
Greetings...
Hello %(name)s!
Your age squared is %(age)s
"""
>>> values = {‘name‘: ‘Bob‘, ‘age‘:40}
>>> print(reply % values)
Greetings...
Hello Bob!
Your age squared is 40
说明:
在预设语句中,可以直接填写%()表达式,格式化字符串,格式如下:
%[(name)][(flags)][width][.precision]typecode
其中"name"可以通过字典进行输出
==============================================================================
>>> template = ‘{0}, {1} and {2}‘
>>> template.format{‘spam‘, ‘ham‘, ‘eggs‘)
‘spam, ham and eggs‘
>>> template = ‘{motto}, {pork} and {food}‘
>>> template.format(motto=‘spam‘, pork=‘ham‘, food=‘eggs‘)
‘spam, ham and eggs‘
>>> template = ‘{motto}, {0} and {food}‘
>>> template.format(‘ham‘, motto=‘spam‘, food=‘eggs‘)
‘spam, ham and eggs‘
说明:
通过.format形式,可以进行输出,注意{}中的指代,数字代表读取format中的位置,string型则代表指代变量
===============================================
>>> import sys
>>> ‘My {1[spam]} runs {0.platform}‘.format(sys, {‘spam‘: ‘laptop‘})
‘My laptop runs win32‘
>>> ‘My {config[spam]} runs {sys.platform}‘.format(sys = sys, config={‘spam‘: ‘laptop‘})
‘My laptop runs win32‘
说明:
可以使用键、属性和偏移量。
注意,在print语句中,填写的sys并不代表程序固定变量,需要通过sys=sys进行赋值
标签:使用 io for ar sp on c python type
原文地址:http://www.cnblogs.com/yy-is-ing/p/3937589.html