Python format字符串格式化函数的使用
作者:木心 时间:2023-12-20 12:45:59
一、简介
从Python2.6开始,新增了str.format()
,它增强了字符串格式化的功能。基本语法是通过 {}
和 :
来代替以前的 %
占位符。
二、占位符%方式
字符串格式符号用法如下
举个例子:
name = 'sugar'
age = 21
print("His name is %s, and he is %d year old." %(name, age))
结果
His name is sugar, and he is 21 year old.
其他格式化辅助操作指令如下,其中用的比较多的就是使用0
来补零,和控制小数位数的.
举个例子:
price = 23.1999
obj = 'milk'
print("The %s's price is %03f" %(obj, price)) # 前面补三个零
print("The %s's price is %3.0f" %(obj, price)) # 最小总占位长度为3,控制输出0个小数
print("The %s's price is %3.3f" %(obj, price)) # 最小总占位长度为3,控制输出3个小数
print("The %s's price is %5.4f" %(obj, price)) # 最小总占位长度为5,控制输出4个小数
结果:
The milk's price is 23.199900The milk's price is 23The milk's price is 23.200The milk's price is 23.1999
三、format格式化方式
字符串format格式化的四种方式
1、使用默认位置方式
格式:string{}.format(x1, x2)
举个例子
price = 23.1999
obj = 'milk'
print("The {}'s price is {}".format(obj, price))
结果如下
The milk's price is 23.1999
2、使用指定位置方式
格式:string{0}.format(x1, x2)
举个例子
price = 23.1999
obj = 'milk'
print("The {0}'s price is {1}".format(obj, price))
结果如下
The milk's price is 23.1999
3、使用列表方式
其实这种方式就相当于前两种使用默认位置和使用指定位置的方式,只不过这里需要使用*
对列表进行解包,举个例子
price = 23.1999
obj = 'milk'
info = [obj, price]
print("The {}'s price is {}".format(*info)) # 对info进行解包
结果如下
The milk's price is 23.1999
4、使用字典的键值对方式
格式:string(key).format(key=value)
举个例子,当然也可以用**
对字典进行解包
price = 23.1999
obj = 'milk'
print("The {name}'s price is {pri}".format(name=obj, pri=price))
# 更进一步,对字典进行解包
dic = {'name':'milk', 'pri':23.1999}
print("The {name}'s price is {pri}".format(**dic))
结果如下
The milk's price is 23.1999
The milk's price is 23.1999
5、其他数字格式化的方式
需要注意的是,在:
冒号后面指定需要填充的内容,可以使用上述4种格式化方式来对文本格式进行控制,举个例子
price = 23.1999
obj = 'bread'
print("The {}'s price is {:.2f}".format(obj, price)) # 使用默认位置方式,保留两位小数
print("The {0}'s price is {1:.2f}".format(obj, price)) # 使用指定位置方式,保留两位小数
print("The {name}'s price is {price:.2f}".format(name=obj, price=price)) # 使用字典方式,保留两位小数
li = [obj, price]
print("The {}'s price is {:.2f}".format(*li)) # 使用列表解包的方式,保留两位小数
info = {'name':obj, 'price':price}
print("The {name}'s price is {price:.2f}".format(**info)) # 使用字典解包的方式,保留两位小数
结果如下
The bread's price is 23.20
The bread's price is 23.20
The bread's price is 23.20
The bread's price is 23.20
The bread's price is 23.20
四、Reference
https://www.runoob.com/python/python-strings.html
来源:https://blog.csdn.net/qq_44940689/article/details/122394153
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现用于测试网站访问速率的方法
python实现文件助手中查看微信撤回消息
PyTorch搭建多项式回归模型(三)
![](https://img.aspxhome.com/file/2023/3/124813_0s.gif)
Python二叉搜索树与双向链表转换实现方法
django文档学习之applications使用详解
![](https://img.aspxhome.com/file/2023/2/107112_0s.png)
python笔记之使用fillna()填充缺失值
![](https://img.aspxhome.com/file/2023/0/135160_0s.png)
保护SQL服务器的安全 用户识别问题
![](https://img.aspxhome.com/file/UploadPic/200812/20081225122629996.jpg)
现代 javscript 编程
![](https://img.aspxhome.com/file/uploadpic/20075/2007511165112755.gif)
三行Python代码提高数据处理脚本速度
![](https://img.aspxhome.com/file/2023/3/90533_0s.png)
python3调用windows dos命令的例子
Python中使用不同编码读写txt文件详解
Python logging日志模块 配置文件方式
学习python之编写简单乘法口诀表实现代码
基于SQLAlchemy实现操作MySQL并执行原生sql语句
微软补丁KB967723造成MySQL频繁无法连接
CentOS 6、7下mysql 5.7 详细安装教程
![](https://img.aspxhome.com/file/2023/9/121309_0s.jpg)
css中使用CLASS来设计网页布局
Python如何读写CSV文件
Dlib+OpenCV深度学习人脸识别的方法示例
![](https://img.aspxhome.com/file/2023/3/72063_0s.png)