網站首頁 編程語言 正文
format用法
?相對基本格式化輸出采用‘%’的方法,format()功能更強大,該函數把字符串當成一個模板,通過傳入的參數進行格式化,并且使用大括號‘{}’作為特殊字符代替‘%’
使用方法由兩種:b.format(a)和format(a,b)。
一、填充
1.無參(1)
print('{} {}'.format('hello','world'))
hello world
2.無參(2)
print('{0} {1}'.format('hello','world'))
hello world
3.無參(3)
print('{1} {0} {1}'.format('hello','world'))
world hello world
4.key value
print('ID:{id},Name:{name}'.format(id='001',name='hello'))
ID:001,Name:hello
5.列表
list=['001','hello'] print('ID:{List[0]},Name:{List[1]}'.format(List = list)) print('ID:{0[0]},Name:{0[1]}'.format(list))
ID:001,Name:hello
ID:001,Name:hello
6.字典
dict={'id':'001,'name':'hello'} print('ID:{Dict[0]},Name:{Dict[1]}'.format(Dict = dict)) print('ID:{id},Name:{name}'.format(**dict))
ID:001,Name:hello
ID:001,Name:hello
7.類
class value(): id = '001' name = 'hello' print('ID:{Value.id},Name{Value.name}'.format(Value = value))
ID:001,Name:hello
8.魔法參數
*args表示任何多個無名參數,它是一個tuple or list;**kwargs表示關鍵字參數,它是一個 dict。
args = [',','.'] kwargs = {'id': '001','name':'hello'} print('ID:{id}{}Name:{name}{}'.format(*args, **kwargs))
ID:001,Name:hello.
二、數字格式化
數字 | 格式 | 輸出 | 描述 |
---|---|---|---|
3.1415926 | {:.2f} | 3.14 | 保留小數點后兩位 |
3.1415926 | {:+.2f} | +3.14 | 帶符號保留小數點后兩位 |
-1 | {:+.2f} | -1.00 | 帶符號保留小數點后兩位 |
2.71828 | {:.0f} | 3 | 不帶小數 |
5 | {:0>2d} | 05 | 數字補零 (填充左邊, 寬度為2) |
5 | {:x<4d} | 5xxx ) | 數字補x (填充右邊, 寬度為4 |
10 | {:x<4d} | 10xx ) | 數字補x (填充右邊, 寬度為4 |
1000000 | {:,} | 1,000,000 | 以逗號分隔的數字格式 |
0.25 | {:.2%} | 25.00% | 百分比格式 |
1000000000 | {:.2e} | 1.00e+09 | 指數記法 |
13 | {:>10d} | 13 | 右對齊 (默認, 寬度為10) |
13 | {:<10d} | 13 | 左對齊 (寬度為10) |
13 | {:^10d} | 13 | 中間對齊 (寬度為10) |
11 | ‘{:b}’.format(11) | 1011 | 二進制 |
11 | ‘{:d}’.format(11) | 11 | 十進制 |
11 | ‘{:o}’.format(11) | 13 | 八進制 //這里打成中文的冒號了,因為用英文的會打出一個O的表情~~~ |
11 | ‘{:x}’.format(11) | b | 十六進制 |
11 | ‘{:#x}’.format(11) | 0xb | 0x式十六進制+小寫 |
11 | ‘{:#X}’.format(11) | 0xB | 0x式十六進制+大寫 |
三、嘆號用法
print(‘{!s}好'.format(‘你')) print(‘{!r}好'.format(‘你')) print(‘{!a}好'.format(‘你'))
你好
’你’好
’\u4f60’好
!后面可以加s r a 分別對應str() repr() ascii() 作用是在填充前先用對應的函數來處理參數
差別就是
str()是面向用戶的,目的是可讀性,
repr()帶有引號,
ascii()是面向Python解析器的,返回值表示在python內部的含義,ascii (),返回ascii編碼
總結
原文鏈接:https://blog.csdn.net/weixin_43347550/article/details/105004905
相關推薦
- 2023-03-16 python中asyncore異步模塊的實現_python
- 2023-07-29 [plugin:vite:import-analysis]Failed to resolve imp
- 2022-06-24 淺談Golang的new與make區別是什么_Golang
- 2022-07-18 進程和計劃任務管理
- 2022-07-12 Linux環境Jenkins部署
- 2023-02-10 docker實現跨宿主機的容器之間網絡互聯_docker
- 2022-12-09 C++中使用cout以hex格式輸出方式_C 語言
- 2022-07-29 詳解Go語言中配置文件使用與日志配置_Golang
- 最近更新
-
- 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同步修改后的遠程分支