網站首頁 編程語言 正文
“%”的使用
格式符 | 描述 |
---|---|
%s | 字符串 (采用str()的顯示) |
%r | 字符串 (采用repr()的顯示) |
%c | 單個字符及其ASCII碼 |
%u | 整數(無符號) |
%b | 二進制整數 |
%o | 八進制數(無符號) |
%d | 十進制整數 |
%i | 十進制整數 |
%x | 十六進制數(無符號) |
%X | 十六進制數大寫(無符號) |
%e | 指數 (基底寫為e),用科學計數法格式化浮點數 |
%E | 指數 (基底寫為E),作用同%e |
%f | 浮點數,可指定小數點后的精度 |
%g | %f和%e的簡寫,指數(e)或浮點數 (根據顯示長度) |
%G | %F和%E的簡寫,指數(E)或浮點數 (根據顯示長度) |
%p | 用十六進制數格式化變量的地址 |
%% | 轉義,字符"%" |
字符串輸出(%s)
- %10s——右對齊,占位符10位
- %-10s——左對齊,占位符10位
- %.2s——截取2位字符串
- %10.2s——10位占位符,截取兩位字符串
# 字符串輸出
print('%s' % 'hello world') # 結果:hello world
# 右對齊,取20位,不夠則補位
print('%20s' % 'hello world') # 結果: hello world
# 左對齊,取20位,不夠則補位
print('%-20s' % 'hello world') # 結果:hello world
# 取2位
print('%.2s' % 'hello world') # 結果:he
# 右對齊,占位符10位,取2位
print('%10.2s' % 'hello world') # 結果: he
# 左對齊,占位符10位,取2位
print('%-10.2s' % 'hello world') # 結果:he
浮點數輸出(%f)
%f ——保留小數點后面六位有效數字
%.3f,保留3位小數位
%e ——保留小數點后面六位有效數字,指數形式輸出
%.3e,保留3位小數位,使用科學計數法
%g ——在保證六位有效數字的前提下,使用小數方式,否則使用科學計數法
%.3g,保留3位有效數字,使用小數或科學計數法
# 默認保留6位小數
print('%f' % 1.11) # 1.110000
# 取1位小數
print('%.1f' % 1.11) # 結果:1.1
# 默認6位小數,用科學計數法
print('%e' % 1.11) # 結果:1.110000e+00
# 取3位小數,用科學計數法
print('%.3e' % 1.11) # 結果:1.110e+00
# 默認6位有效數字
print('%g' % 1111.1111) # 結果:1111.11
# 取7位有效數字
print('%.7g' % 1111.1111) # 結果:1111.111
# 取2位有效數字,自動轉換為科學計數法
print('%.2g' % 1111.1111) # 結果:1.1e+03
format的使用
位置匹配
① 不帶參數,即{}
② 帶數字參數,可調換順序,即{1}、{2}
③ 帶關鍵字,即{a}、{to}
# 不帶參數
print('{} {}'.format('hello','world')) # 結果:hello world
# 帶數字參數
print('{0} {1}'.format('hello','world')) # 結果:hello world
# 參數順序倒亂
print('{0} {1} {0}'.format('hello','world')) # 結果:hello world hello
# 帶關鍵字參數
print('{a} {tom} {a}'.format(tom='hello',a='world')) # 結果:world hello world
# 通過索引
coord = (3, 5)
print('X: {0[0]}; Y: {0[1]}'.format(coord)) # 結果:'X: 3; Y: 5'
# 通過key鍵參數
a = {'a': 'test_a', 'b': 'test_b'}
print('X: {0[a]}; Y: {0[b]}'.format(a)) # 結果:'X: test_a; Y: test_b'
格式轉換
符號 | 描述 |
---|---|
'b' | 二進制。將數字以2為基數進行輸出 |
'c' | 字符。在打印之前將整數轉換成對應的Unicode字符串 |
'd' | 十進制整數。將數字以10為基數進行輸出 |
'o' | 八進制。將數字以8為基數進行輸出 |
'x' | 十六進制。將數字以16為基數進行輸出,9以上的位數用小寫字母 |
'e' | 冪符號。用科學計數法打印數字。用'e'表示冪 |
'g' | 一般格式。將數值以fixed-point格式輸出。當數值特別大的時候,用冪形式打印 |
'n' | 數字。當值為整數時和'd'相同,值為浮點數時和'g'相同。不同的是它會根據區域設置插入數字分隔符 |
'%' | 百分數。將數值乘以100然后以fixed-point('f')格式打印,值后面會有一個百分號 |
print('{0:b}'.format(3)) # 結果:11
print('{:c}'.format(20)) # 結果:?
print('{:d}'.format(20)) # 結果:20
print('{:o}'.format(20)) # 結果:24
print('{:x}'.format(20)) # 結果:14
print('{:e}'.format(20)) # 結果:2.000000e+01
print('{:g}'.format(20.1)) # 結果:20.1
print('{:f}'.format(20)) # 結果:20.000000
print('{:n}'.format(20)) # 結果:20
print('{:%}'.format(20)) # 結果:2000.000000%
高階用法
進制轉換
print("int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42))
# 輸出:int: 42; hex: 2a; oct: 52; bin: 101010
print("int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42))
# 在前面加“#”,則帶進制前綴
# 輸出:int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010
左中右對齊及位數補全㈠ 對齊
符號 | 描述 |
---|---|
< | 左對齊(默認) |
> | 右對齊 |
^ | 居中對齊 |
= | 在小數點后進行補齊(只用于數字) |
㈡ 取位數 “{:4s}”、"{:.2f}"等
# 默認左對齊
print('{} and {}'.format('hello','world')) # 結果:hello and world
# 取10位左對齊,取10位右對齊
print('{:10s} and {:>10s}'.format('hello','world')) # 結果:hello and world
# 取10位中間對齊
print('{:^10s} and {:^10s}'.format('hello','world')) # 結果: hello and world
# 取2位小數
print('{} is {:.2f}'.format(1.123,1.123)) # 結果:1.123 is 1.12
# 取2位小數,右對齊,取10位
print('{0} is {0:>10.2f}'.format(1.123)) # 結果:1.123 is 1.12
# 左對齊
print('{:<30}'.format('left aligned')) # 結果:'left aligned '
# 右對齊
print('{:>30}'.format('right aligned')) # 結果:' right aligned'
# 中間對齊
print('{:^30}'.format('centered')) # 結果:' centered '
# 使用“*”填充
print('{:*^30}'.format('centered')) # 結果:'***********centered***********'
# 還有“=”只能應用于數字,這種方法可用“>”代替
print('{:0=30}'.format(11)) # '000000000000000000000000000011'
正負符號顯示正負符號顯示 %+f, %-f, 和 % f的用法
# 總是顯示符號
print('{:+f}; {:+f}'.format(3.14, -3.14)) # '+3.140000; -3.140000'
# 若是+數,則在前面留空格
print('{: f}; {: f}'.format(3.14, -3.14)) # ' 3.140000; -3.140000'
# -數時顯示-,與'{:f}; {:f}'一致
print('{:-f}; {:-f}'.format(3.14, -3.14)) # '3.140000; -3.140000'
百分數%
points = 19
total = 22
print('Correct answers: {:.2%}'.format(points/total)) # 'Correct answers: 86.36%'
逗號作為千位分隔符,金額表示
print('{:,}'.format(1234567890)) # '1,234,567,890'
format變形用法
在字符串前加f以達到格式化的目的,在{}里加入對象,此為format的另一種形式
name = 'jack'
age = 18
sex = 'man'
job = "IT"
salary = 9999.99
print(f'my name is {name.capitalize()}.') # my name is Jack.
print(f'I am {age:*^10} years old.') # I am ****18**** years old.
print(f'I am a {sex}') # I am a man
print(f'My salary is {salary:10.3f}') # My salary is 9999.990
原文鏈接:https://www.jianshu.com/p/617cc100b1bf
相關推薦
- 2022-06-22 Android?ViewPager實現頁面左右切換效果_Android
- 2022-10-25 Go語言實戰學習之流程控制詳解_Golang
- 2022-06-30 Oracle中的觸發器trigger_oracle
- 2022-10-31 理解k8s控制器DaemonSet創建及使用場景_云其它
- 2022-04-16 Python3如何將源目錄中的圖片用MD5命名并可以設定目標目錄_python
- 2022-11-12 C++?哈希表的基本用法及說明_C 語言
- 2024-01-31 成功解決 npm ERR! /usr/bin/git ls-remote -h -t git://g
- 2022-07-14 Jquery+Ajax實現跨域訪問_jquery
- 最近更新
-
- 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同步修改后的遠程分支