網站首頁 編程語言 正文
一、簡介
從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.199900 The milk's price is ?23 The milk's price is 23.200 The 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://blog.csdn.net/qq_44940689/article/details/122394153
相關推薦
- 2022-04-17 css absolute絕對定位 讓 top 和bottom 同時生效
- 2022-11-10 一文詳解Redis中的持久化_Redis
- 2022-06-01 關于nginx?反向代理?URL替換方案_nginx
- 2023-04-08 Swift?HTTP加載請求Loading?Requests教程_Swift
- 2022-12-21 OpenHarmony實現屏幕亮度動態調節方法詳解_Android
- 2022-08-01 為MongoDB數據庫注冊windows服務_MongoDB
- 2022-04-27 Python?Pandas學習之基本數據操作詳解_python
- 2022-06-19 docker容器非root用戶提權的問題解決_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支