網站首頁 編程語言 正文
1.format() 基本用法
-
python2.6
開始,新增了一種格式化字符串的函數str.format()
, - 它增強了字符串格式化的功能
- 基本語法是通過{} 和 : 來代替以前的 % .
-
format
函數可以接受不限個參數,位置可以不按順序。
a = "姓名:{0},年齡:{1}" print(a.format("小明",18)) ? b = "姓名:{0},年齡:{1},{0}是個學生" print(b.format("小明",18)) ? c = "姓名:{name},年齡:{age}" print(c.format(age=19,name="小明"))
可以通過{索引}/{參數名},直接映射參數值,實現對字符串的格式化;
2.填充與對齊
- 填充跟對齊一起使用
- ^,<,> 分別是居中,左對齊,右對齊,后面帶寬度
- # :號后面帶填充的字符,只能是一個字符,不指定的話默認是用空格填充
print("{:*>8}".format("245")) ? print("我是{0},我喜歡語文{1:*<8}".format("小明","666")) ? print("我是{0},我喜歡語文{1:*>8}".format("小明","666"))
3.數字格式化
- # 浮點數通過 f,整數通過 d 進行需要的格式化。
a = "{0},錢:{1:.2f}" print(a.format("小明",3333.23456))
test_0="{0:.2f}" print(test_0.format(3.1415926)) ? test_1="{0:+.2f}" print(test_1.format(3.1415926)) ? test_2="{0:.0f}" print(test_2.format(3.1415926)) ? test_3="{0:0>2d}" print(test_3.format(5)) ? test_4="{0:x<4d}" print(test_4.format(5)) ? test_5="{0:,}" print(test_5.format(1000000)) ? test_6="{0:.2%}" print(test_6.format(0.25)) ? test_7="{0:.2e}" print(test_7.format(10000000000)) ? test_8="{0:10d}" print(test_8.format(13)) ? test_9="{0:<10d}" print(test_9.format(13)) ? test_10="{0:^10d}" print(test_10.format(13))
原文鏈接:https://blog.csdn.net/qq_26086231/article/details/123170137
相關推薦
- 2022-05-12 Kotlin 接口 interface 默認實現了open。并且可以提供默認實現
- 2022-11-17 React路由渲染方式與withRouter高階組件及自定義導航組件應用詳細介紹_React
- 2023-03-11 Python基礎教程之增加和去除數字的千位分隔符_python
- 2022-11-20 Go語言操作Excel利器之excelize類庫詳解_Golang
- 2022-05-15 C++的數據共享與保護你了解嗎_C 語言
- 2022-11-25 PostgreSQL自增主鍵用法及在mybatis中的使用教程_PostgreSQL
- 2022-11-04 ASP.NET?MVC擴展帶驗證的單選按鈕_實用技巧
- 2022-07-19 Tomcat升級版本出現400問題
- 最近更新
-
- 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同步修改后的遠程分支