看代码学编程之python字符串格式化

来源:互联网 发布:淘宝主图分辨率是多少 编辑:程序博客网 时间:2024/05/16 19:14
#!/usr/bin/env python# -*- coding:utf-8 -*-#字符串填充tpl = 'i am %s'  % 'yongge'print(tpl)tpl = 'i am %s i love %s'  % ('yongge','python')print(tpl)# 字典填充1tpl = '1 am %(name)s ,my age is %(age)d' %{'name':'yongge','age':25};print(tpl)#字典填充2tpl = '1 am {name} ,my age is {age}'.format(**{'name':'yongge','age':25});print(tpl)#字典填充3tpl = '1 am {name:s} ,my age is {age:d}'.format(name='yongge',age=25);print(tpl)#列表填充3tpl = '1 am {:s} ,my age is {:d}'.format(*['yongge',25]);print(tpl)#加颜色tpl = '1 am \033[41;1m   %(name)s   \033[0m ,my age is %(age)d' %{'name':'yongge','age':25};print(tpl)tpl = '1 am {} i love {}'.format('yongge','python')print(tpl)tpl = '1 am {1} i love {0}'.format('yongge','python')print(tpl)#保留2位tpl = '同期增长%.2f%%' %34.44545print(tpl)#截取两个字符tpl = '同期增长%.2s%%' %34.44545print(tpl)
原创粉丝点击