python格式化转换_(转)python 格式化输出及%用法
一、格式化輸出1、整數(shù)的輸出%o —— oct 八進(jìn)制%d —— dec 十進(jìn)制%x —— hex 十六進(jìn)制
print('%o' % 20)24
print('%d' % 20)20
print('%x' % 20)14
2、浮點數(shù)輸出(1)格式化輸出%f ——保留小數(shù)點后面六位有效數(shù)字 %.3f,保留3位小數(shù)位%e ——保留小數(shù)點后面六位有效數(shù)字,指數(shù)形式輸出 %.3e,保留3位小數(shù)位,使用科學(xué)計數(shù)法%g ——在保證六位有效數(shù)字的前提下,使用小數(shù)方式,否則使用科學(xué)計數(shù)法 %.3g,保留3位有效數(shù)字,使用小數(shù)或科學(xué)計數(shù)法
>>> print('%f' % 1.11) #默認(rèn)保留6位小數(shù)
1.110000
>>> print('%.1f' % 1.11) #取1位小數(shù)
1.1
>>> print('%e' % 1.11) #默認(rèn)6位小數(shù),用科學(xué)計數(shù)法
1.110000e+00
>>> print('%.3e' % 1.11) #取3位小數(shù),用科學(xué)計數(shù)法
1.110e+00
>>> print('%g' % 1111.1111) #默認(rèn)6位有效數(shù)字
1111.11
>>> print('%.7g' % 1111.1111) #取7位有效數(shù)字
1111.111
>>> print('%.2g' % 1111.1111) #取2位有效數(shù)字,自動轉(zhuǎn)換為科學(xué)計數(shù)法
1.1e+03
(2)內(nèi)置round()
round(number[, ndigits])參數(shù):number?- 這是一個數(shù)字表達(dá)式。ndigits?- 表示從小數(shù)點到最后四舍五入的位數(shù)。默認(rèn)值為0。返回值該方法返回x的小數(shù)點舍入為n位數(shù)后的值。
round()函數(shù)只有一個參數(shù),不指定位數(shù)的時候,返回一個整數(shù),而且是最靠近的整數(shù),類似于四舍五入,當(dāng)指定取舍的小數(shù)點位數(shù)的時候,一般情況也是使用四舍五入的規(guī)則,但是碰到.5的情況時,如果要取舍的位數(shù)前的小數(shù)是奇數(shù),則直接舍棄,如果是偶數(shù)則向上取舍。
注:“.5”這個是一個“坑”,且python2和python3出來的接口有時候是不一樣的,盡量避免使用round()函數(shù)吧
>>> round(1.1125) #四舍五入,不指定位數(shù),取整
1
>>> round(1.1135,3) #取3位小數(shù),由于3為奇數(shù),則向下“舍”
1.113
>>> round(1.1125,3) #取3位小數(shù),由于2為偶數(shù),則向上“入”
1.113
>>> round(1.5) #無法理解,查閱一些資料是說python會對數(shù)據(jù)進(jìn)行截斷,沒有深究
2
>>> round(2.5) #無法理解
2
>>> round(1.675,2) #無法理解
1.68
>>> round(2.675,2) #無法理解
2.67
>>>
3、字符串輸出%s%10s——右對齊,占位符10位%-10s——左對齊,占位符10位%.2s——截取2位字符串%10.2s——10位占位符,截取兩位字符串
>>> print('%s' % 'hello world') #字符串輸出
hello world>>> print('%20s' % 'hello world') #右對齊,取20位,不夠則補(bǔ)位
hello world>>> print('%-20s' % 'hello world') #左對齊,取20位,不夠則補(bǔ)位
hello world>>> print('%.2s' % 'hello world') #取2位
he>>> print('%10.2s' % 'hello world') #右對齊,取2位
he>>> print('%-10.2s' % 'hello world') #左對齊,取2位
he
4、 其他
字符串格式代碼如下
(2)常用轉(zhuǎn)義字符如下
二、format用法
相對基本格式化輸出采用‘%’的方法,format()功能更強(qiáng)大,該函數(shù)把字符串當(dāng)成一個模板,通過傳入的參數(shù)進(jìn)行格式化,并且使用大括號‘{}’作為特殊字符代替‘%’
使用方法由兩種:b.format(a)和format(a,b)。
1、基本用法
(1)不帶編號,即“{}”
(2)帶數(shù)字編號,可調(diào)換順序,即“{1}”、“{2}”
(3)帶關(guān)鍵字,即“{a}”、“{tom}”
>>> print('{} {}'.format('hello','world')) #不帶字段
hello world>>> print('{0} {1}'.format('hello','world')) #帶數(shù)字編號
hello world>>> print('{0} {1} {0}'.format('hello','world')) #打亂順序
hello world hello>>> print('{1} {1} {0}'.format('hello','world'))
world world hello>>> print('{a} {tom} {a}'.format(tom='hello',a='world')) #帶關(guān)鍵字
world hello world
2、進(jìn)階用法
(1)< (默認(rèn))左對齊、> 右對齊、^ 中間對齊、= (只用于數(shù)字)在小數(shù)點后進(jìn)行補(bǔ)齊
(2)取位數(shù)“{:4s}”、"{:.2f}"等
>>> print('{} and {}'.format('hello','world')) #默認(rèn)左對齊
hello andworld>>> print('{:10s} and {:>10s}'.format('hello','world')) #取10位左對齊,取10位右對齊
hello andworld>>> print('{:^10s} and {:^10s}'.format('hello','world')) #取10位中間對齊
hello andworld>>> print('{} is {:.2f}'.format(1.123,1.123)) #取2位小數(shù)
1.123 is 1.12
>>> print('{0} is {0:>10.2f}'.format(1.123)) #取2位小數(shù),右對齊,取10位
1.123 is 1.12
3、多個格式化
'b' - 二進(jìn)制。將數(shù)字以2為基數(shù)進(jìn)行輸出。
'c' - 字符。在打印之前將整數(shù)轉(zhuǎn)換成對應(yīng)的Unicode字符串。
'd' - 十進(jìn)制整數(shù)。將數(shù)字以10為基數(shù)進(jìn)行輸出。
'o' - 八進(jìn)制。將數(shù)字以8為基數(shù)進(jìn)行輸出。
'x' - 十六進(jìn)制。將數(shù)字以16為基數(shù)進(jìn)行輸出,9以上的位數(shù)用小寫字母。
'e' - 冪符號。用科學(xué)計數(shù)法打印數(shù)字。用'e'表示冪。
'g' - 一般格式。將數(shù)值以fixed-point格式輸出。當(dāng)數(shù)值特別大的時候,用冪形式打印。
'n' - 數(shù)字。當(dāng)值為整數(shù)時和'd'相同,值為浮點數(shù)時和'g'相同。不同的是它會根據(jù)區(qū)域設(shè)置插入數(shù)字分隔符。
'%' - 百分?jǐn)?shù)。將數(shù)值乘以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%
>>>
總結(jié)
以上是生活随笔為你收集整理的python格式化转换_(转)python 格式化输出及%用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cs精英游戏python代码_pytho
- 下一篇: js字符串拼接中关于单引号和双引号的那些