[python]format的使用

来源:互联网 发布:苗勒管永存综合征 知乎 编辑:程序博客网 时间:2024/05/18 19:21
举例1:  使用{i}指定取哪个参数
>>> name="lucy">>> age=18>>> print "This is {0}, aged {1}".format(name, age)      This is lucy, aged 18
{0}表示取第0个参数,{1}取第1个参数,分别对应name,age变量的值,从下例感受一下:
>>> print "This is {1}, aged {1}".format(name, age)    This is 18, aged 18
>>> print "This is {0}, aged {0}".format(name, age)This is lucy, aged lucy
举例2:   使用{key}指定取哪个参数
>>> print "This is {name}, aged {age}".format(name='lily', age=10)     第一个key是name,后面对这个key赋值This is lily, aged 10
原创粉丝点击