Python format字符串格式化函数的使用

作者:木心 时间:2023-12-20 12:45:59 

一、简介

从Python2.6开始,新增了str.format(),它增强了字符串格式化的功能。基本语法是通过 {}: 来代替以前的 占位符。

二、占位符%方式

字符串格式符号用法如下

Python 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来补零,和控制小数位数的.

Python format字符串格式化函数的使用

举个例子:

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、其他数字格式化的方式

Python format字符串格式化函数的使用

需要注意的是,在:冒号后面指定需要填充的内容,可以使用上述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

标签:Python,format,字符串,格式化
0
投稿

猜你喜欢

  • python实现用于测试网站访问速率的方法

    2023-07-28 19:12:02
  • python实现文件助手中查看微信撤回消息

    2023-09-03 07:23:50
  • PyTorch搭建多项式回归模型(三)

    2022-09-04 00:43:49
  • Python二叉搜索树与双向链表转换实现方法

    2022-08-23 12:46:34
  • django文档学习之applications使用详解

    2021-09-11 11:15:12
  • python笔记之使用fillna()填充缺失值

    2023-12-22 19:53:49
  • 保护SQL服务器的安全 用户识别问题

    2008-12-24 15:26:00
  • 现代 javscript 编程

    2011-04-21 16:24:00
  • 三行Python代码提高数据处理脚本速度

    2021-10-15 22:56:54
  • python3调用windows dos命令的例子

    2021-11-10 06:36:44
  • Python中使用不同编码读写txt文件详解

    2023-06-29 20:35:27
  • Python logging日志模块 配置文件方式

    2021-03-07 04:31:01
  • 学习python之编写简单乘法口诀表实现代码

    2021-11-12 20:19:20
  • 基于SQLAlchemy实现操作MySQL并执行原生sql语句

    2024-01-18 17:16:05
  • 微软补丁KB967723造成MySQL频繁无法连接

    2009-11-03 14:28:00
  • CentOS 6、7下mysql 5.7 详细安装教程

    2024-01-24 18:00:47
  • css中使用CLASS来设计网页布局

    2007-10-30 13:01:00
  • Python如何读写CSV文件

    2023-03-23 08:41:13
  • Dlib+OpenCV深度学习人脸识别的方法示例

    2022-11-08 06:34:42
  • 关闭窗口时保存数据的办法

    2009-02-19 13:39:00
  • asp之家 网络编程 m.aspxhome.com