看代码学编程之python字符串格式化
#!/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' %
·
#!/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)
# 字典填充1
tpl = '1 am %(name)s ,my age is %(age)d' %{'name':'yongge','age':25};
print(tpl)
#字典填充2
tpl = '1 am {name} ,my age is {age}'.format(**{'name':'yongge','age':25});
print(tpl)
#字典填充3
tpl = '1 am {name:s} ,my age is {age:d}'.format(name='yongge',age=25);
print(tpl)
#列表填充3
tpl = '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.44545
print(tpl)
#截取两个字符
tpl = '同期增长%.2s%%' %34.44545
print(tpl)
更多推荐
已为社区贡献4条内容
所有评论(0)