Python自学笔记
来源:互联网 发布:超级红包群txt下载知新 编辑:程序博客网 时间:2024/06/01 07:11
在Python中,采用的格式化方式和C语言是一致的,用%
实现,举例如下:
>>> 'Hello, %s' % 'world''Hello, world'>>> 'Hi, %s, you have $%d.' % ('Michael', 1000000)'Hi, Michael, you have $1000000.'
常见的占位符有: %d
整数 %f
浮点数 %s
字符串 %x
十六进制整数
其中,格式化整数和浮点数还可以指定是否补0和整数与小数的位数:
>>> '%2d-%02d' % (3, 1) #注意2和02的区别' 3-01'>>> '%.2f' % 3.1415926'3.14'
如果你不太确定应该用什么,%s永远起作用,它会把任何数据类型转换为字符串:
>>> 'Age: %s. Gender: %s' % (25, True)'Age: 25. Gender: True'
有些时候,字符串里面的%是一个普通字符怎么办?这个时候就需要转义,用%%来表示一个%:
>>> 'growth rate: %d %%' % 7'growth rate: 7 %'
阅读全文
0 0
- python自学笔记1
- python自学笔记 2
- python自学笔记 3
- python自学笔记 4
- python自学笔记
- Python自学笔记
- python自学笔记 20151126
- python 自学笔记
- python 自学笔记20151215
- python 自学笔记20151222
- python 自学笔记
- python 自学笔记 20151224
- Python自学笔记20160620
- PYTHON自学笔记1
- python自学笔记
- python自学笔记
- Python自学笔记
- Python自学笔记1
- 文章标题
- 决策树算法及实现
- 面试OR笔试24——翻转单词顺序
- python入门基础函数和匿名函数
- 微服务知识图谱
- Python自学笔记
- 大话计算机网络
- java前端技术---jquery基础详解
- servlet的执行原理与生命周期
- github 项目上传
- virtualbox安装centos7
- 选择排序
- Redis原理
- UVA