網(wǎng)站首頁 編程語言 正文
format用法
?相對基本格式化輸出采用‘%’的方法,format()功能更強(qiáng)大,該函數(shù)把字符串當(dāng)成一個模板,通過傳入的參數(shù)進(jìn)行格式化,并且使用大括號‘{}’作為特殊字符代替‘%’
使用方法由兩種: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.魔法參數(shù)
*args表示任何多個無名參數(shù),它是一個tuple or list;**kwargs表示關(guān)鍵字參數(shù),它是一個 dict。
args = [',','.'] kwargs = {'id': '001','name':'hello'} print('ID:{id}{}Name:{name}{}'.format(*args, **kwargs))
ID:001,Name:hello.
二、數(shù)字格式化
數(shù)字 | 格式 | 輸出 | 描述 |
---|---|---|---|
3.1415926 | {:.2f} | 3.14 | 保留小數(shù)點(diǎn)后兩位 |
3.1415926 | {:+.2f} | +3.14 | 帶符號保留小數(shù)點(diǎn)后兩位 |
-1 | {:+.2f} | -1.00 | 帶符號保留小數(shù)點(diǎn)后兩位 |
2.71828 | {:.0f} | 3 | 不帶小數(shù) |
5 | {:0>2d} | 05 | 數(shù)字補(bǔ)零 (填充左邊, 寬度為2) |
5 | {:x<4d} | 5xxx ) | 數(shù)字補(bǔ)x (填充右邊, 寬度為4 |
10 | {:x<4d} | 10xx ) | 數(shù)字補(bǔ)x (填充右邊, 寬度為4 |
1000000 | {:,} | 1,000,000 | 以逗號分隔的數(shù)字格式 |
0.25 | {:.2%} | 25.00% | 百分比格式 |
1000000000 | {:.2e} | 1.00e+09 | 指數(shù)記法 |
13 | {:>10d} | 13 | 右對齊 (默認(rèn), 寬度為10) |
13 | {:<10d} | 13 | 左對齊 (寬度為10) |
13 | {:^10d} | 13 | 中間對齊 (寬度為10) |
11 | ‘{:b}’.format(11) | 1011 | 二進(jìn)制 |
11 | ‘{:d}’.format(11) | 11 | 十進(jìn)制 |
11 | ‘{:o}’.format(11) | 13 | 八進(jìn)制 //這里打成中文的冒號了,因?yàn)橛糜⑽牡臅虺鲆粋€O的表情~~~ |
11 | ‘{:x}’.format(11) | b | 十六進(jìn)制 |
11 | ‘{:#x}’.format(11) | 0xb | 0x式十六進(jìn)制+小寫 |
11 | ‘{:#X}’.format(11) | 0xB | 0x式十六進(jìn)制+大寫 |
三、嘆號用法
print(‘{!s}好'.format(‘你')) print(‘{!r}好'.format(‘你')) print(‘{!a}好'.format(‘你'))
你好
’你’好
’\u4f60’好
!后面可以加s r a 分別對應(yīng)str() repr() ascii() 作用是在填充前先用對應(yīng)的函數(shù)來處理參數(shù)
差別就是
str()是面向用戶的,目的是可讀性,
repr()帶有引號,
ascii()是面向Python解析器的,返回值表示在python內(nèi)部的含義,ascii (),返回ascii編碼
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_43347550/article/details/105004905
相關(guān)推薦
- 2022-11-03 Python入門教程之三元運(yùn)算符的使用詳解_python
- 2022-07-29 Linux進(jìn)程管理方法介紹_linux shell
- 2022-03-30 C語言關(guān)鍵字之a(chǎn)uto?register詳解_C 語言
- 2022-09-01 一文搞懂c++中的std::move函數(shù)_C 語言
- 2023-02-03 Linux設(shè)置每晚定時備份Oracle數(shù)據(jù)表的操作命令_linux shell
- 2022-08-19 Go語言fsnotify接口實(shí)現(xiàn)監(jiān)測文件修改_Golang
- 2022-06-29 Tomcat配置訪問日志和線程數(shù)的實(shí)現(xiàn)步驟_Tomcat
- 2022-06-12 C語言棧與隊(duì)列相互實(shí)現(xiàn)詳解_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支