python中强大的format函数实例详解
Python中的`format`函数是一个非常强大且灵活的字符串格式化工具,它自Python 2.6版本开始引入,替代了传统的百分比(`%`)格式化方法。`format`函数允许我们通过位置参数、关键字参数、映射(列表或字典)以及对齐和精度控制等多种方式来定制字符串的格式。 **位置参数**: 位置参数是通过索引值来指定要插入到格式化字符串中的值。例如: ```python print('{0},{1}'.format('chuhao', 20)) ``` 在这里,`{0}`和`{1}`分别对应于`format`函数调用中的第一个和第二个参数。 **关键字参数**: 通过关键字参数,我们可以使用变量名来指定值。这对于有多个相同类型的数据需要插入时特别有用,避免了混淆: ```python print('{name},{age}'.format(age=18, name='chuhao')) ``` **对象属性**: `format`函数还可以处理类实例,直接访问其属性: ```python class Person: def __init__(self, name, age): self.name = name self.age = age print(str(Person('chuhao', 18))) # This guy is chuhao,is 18 old ``` 在这里,`{self.name}`和`{self.age}`会被替换为`Person`实例的`name`和`age`属性值。 **映射参数**: 我们可以使用列表或字典来映射值,通过索引或键来引用它们: ```python lista_list = ['chuhao', 20, 'china'] print('my name is {0[0]}, from {0[2]}, age is {0[1]}'.format(lista_list)) b_dict = {'name': 'chuhao', 'age': 20, 'province': 'shanxi'} print('my name is {name}, age is {age}, from {province}'.format(**b_dict)) ``` **对齐和填充**: `format`函数可以控制输出字段的宽度和填充字符。例如: ```python print('{:>8}'.format('189')) # 右对齐,空格填充 print('{:0>8}'.format('189')) # 右对齐,零填充 print('{:a>8}'.format('189')) # 右对齐,'a'字符填充 ``` **精度和类型控制**: 可以使用冒号(`:`)后跟精度来控制浮点数的小数位数,或者使用类型代码来控制整数的表示形式: ```python print('{:.2f}'.format(321.33345)) # 保留两位小数 print('{:,}'.format(1234567890)) # 千位分隔符 ``` 类型代码如`f`(浮点数)、`b`(二进制)、`d`(十进制)、`o`(八进制)和`x`(十六进制): ```python print('{:b}'.format(18)) # 二进制:10010 print('{:d}'.format(18)) # 十进制:18 print('{:o}'.format(18)) # 八进制:22 print('{:x}'.format(18)) # 十六进制:12 ``` Python的`format`函数提供了丰富的字符串格式化选项,使得在编程中创建和控制字符串变得更加简单和直观。无论是简单的数据插入还是复杂的格式调整,`format`函数都能胜任。通过熟练掌握这些用法,可以显著提高代码的可读性和维护性。在编写Python代码时,充分利用`format`函数能帮助我们写出更加清晰和高效的代码。
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 12
- 资源: 904
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)