python字符串格式化
2018-07-24 08:03:02来源:博客园 阅读 ()
字符串格式化
1 # msg='i am %s my hobby is %s' % ('lhf','alex') 2 # print(msg) 3 # 4 # msg='i am %s my hobby is %s' % ('lhf',1) 5 # msg='i am %s my hobby is %s' % ('lhf',[1,2]) 6 # print(msg) 7 # name='lhf' 8 # age=19 9 # msg='i am %s my hobby is %s' % (name,age) 10 # print(msg) 11 12 #打印浮点数 13 # tpl = "percent %.2f" % 99.976234444444444444 14 # print(tpl) 15 16 #打印百分比 17 # tpl = 'percent %.2f %%' % 99.976234444444444444 18 # print(tpl) 19 20 # tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18} 21 # print(tpl) 22 23 # msg='i am %(name)+5s my hobby is alex' %{'name':'lhf'} 24 # print(msg) 25 # 26 # msg='i am \033[43;1m%(name)+60s\033[0m my hobby is alex' %{'name':'lhf'} 27 # print(msg) 28 29 30 # print('root','x','0','0',sep=':') 31 # print('root'+':'+'x'+':'+'0','0')
格式化函数format()
1 # tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) 2 # print(tpl) 3 # 4 # tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) 5 # print(tpl) 6 # tpl = "i am {:s}, age {:d}".format(*["seven", 18]) 7 # print(tpl) 8 # tpl = "i am {:s}, age {:d}".format("seven", 18) #["seven", 18] 9 # print(tpl) 10 # 11 # l=["seven", 18] 12 # tpl = "i am {:s}, age {:d}".format('seven',18) 13 # print(tpl) 14 15 # tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87623, 2) 16 # print(tpl)
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash