Kevin12151011的个人博客分享 http://blog.sciencenet.cn/u/Kevin12151011

博文

Python格式化字符串的两种形式

已有 2029 次阅读 2018-10-7 12:06 |个人分类:Python编程|系统分类:科研笔记

1. %格式,如 ‘%.2f’%(a)

2. .format格式,如 '{:.2f}.format(a)'


其中.format更强大些。比如输出给定精度 precision 的浮点数a,可写为

'{:.{:d}f}.format(a,precision)'

但是%格式做不到。



https://blog.sciencenet.cn/blog-3296568-1139357.html

上一篇:Python faster [不定期更新]
下一篇:Writing BZERO & BSCALE cards to fits headers using astropy
收藏 IP: 218.94.142.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-16 20:55

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部