您当前的位置: 首页 >  Python

少林码僧

暂无认证

  • 0浏览

    0关注

    317博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

少林码僧 发布时间:2017-07-09 13:31:30 ,浏览量:0

#!/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)
关注
打赏
1661398670
查看更多评论
立即登录/注册

微信扫码登录

0.1734s