python输出格式化及函数format
生活随笔
收集整理的這篇文章主要介紹了
python输出格式化及函数format
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
返回博客列表
原?python輸出格式化及函數(shù)format ? Dyllian
返回博客列表
原?python輸出格式化及函數(shù)format ? Dyllian
- 發(fā)布時(shí)間:?2013/05/28 10:31?
- 閱讀:?29711?
- 收藏:?7?
- 點(diǎn)贊:?1?
- 評(píng)論:?2
總結(jié)了一些簡(jiǎn)單基本的輸出格式化形式以及函數(shù)format函數(shù)基本使用形式。
字符串格式化代碼:
| %% | 百分號(hào)標(biāo)記 |
| %c | 字符及其ASCII碼 |
| %s | 字符串 |
| %d | 有符號(hào)整數(shù)(十進(jìn)制) |
| %u | 無符號(hào)整數(shù)(十進(jìn)制) |
| %o | 無符號(hào)整數(shù)(八進(jìn)制) |
| %x | 無符號(hào)整數(shù)(十六進(jìn)制) |
| %X | 無符號(hào)整數(shù)(十六進(jìn)制大寫字符) |
| %e | 浮點(diǎn)數(shù)字(科學(xué)計(jì)數(shù)法) |
| %E | 浮點(diǎn)數(shù)字(科學(xué)計(jì)數(shù)法,用E代替e) |
| %f | 浮點(diǎn)數(shù)字(用小數(shù)點(diǎn)符號(hào)) |
| %g | 浮點(diǎn)數(shù)字(根據(jù)值的大小采用%e或%f) |
| %G | 浮點(diǎn)數(shù)字(類似于%g) |
| %p | 指針(用十六進(jìn)制打印值的內(nèi)存地址) |
| %n | 存儲(chǔ)輸出字符的數(shù)量放進(jìn)參數(shù)列表的下一個(gè)變量中 |
- 發(fā)布時(shí)間:?2013/05/28 10:31?
- 閱讀:?29711?
- 收藏:?7?
- 點(diǎn)贊:?1?
- 評(píng)論:?2
總結(jié)了一些簡(jiǎn)單基本的輸出格式化形式以及函數(shù)format函數(shù)基本使用形式。
字符串格式化代碼:
| %% | 百分號(hào)標(biāo)記 |
| %c | 字符及其ASCII碼 |
| %s | 字符串 |
| %d | 有符號(hào)整數(shù)(十進(jìn)制) |
| %u | 無符號(hào)整數(shù)(十進(jìn)制) |
| %o | 無符號(hào)整數(shù)(八進(jìn)制) |
| %x | 無符號(hào)整數(shù)(十六進(jìn)制) |
| %X | 無符號(hào)整數(shù)(十六進(jìn)制大寫字符) |
| %e | 浮點(diǎn)數(shù)字(科學(xué)計(jì)數(shù)法) |
| %E | 浮點(diǎn)數(shù)字(科學(xué)計(jì)數(shù)法,用E代替e) |
| %f | 浮點(diǎn)數(shù)字(用小數(shù)點(diǎn)符號(hào)) |
| %g | 浮點(diǎn)數(shù)字(根據(jù)值的大小采用%e或%f) |
| %G | 浮點(diǎn)數(shù)字(類似于%g) |
| %p | 指針(用十六進(jìn)制打印值的內(nèi)存地址) |
| %n | 存儲(chǔ)輸出字符的數(shù)量放進(jìn)參數(shù)列表的下一個(gè)變量中 |
基本方法的使用:
#coding=utf-8 ''' 可以指定所需長(zhǎng)度的字符串的對(duì)齊方式: < (默認(rèn))左對(duì)齊 > 右對(duì)齊 ^ 中間對(duì)齊 = (只用于數(shù)字)在小數(shù)點(diǎn)后進(jìn)行補(bǔ)齊 ''' print '1:\t|{0:>10},'.format('wangyu') print '2:\t|{0:4.2f}'.format(1.1415926) print '3:\t|',format(1.1415926,'<10.2f') print '4:\t|{0:<10},{1:<15}'.format('wangyu',1.1415926) print '5:\t|User ID: {uid} Last seen: {last_login}'.format(uid='root',last_login = '5 Mar 2008 07:20') '''格式化指示符可以包含一個(gè)展示類型來控制格式。 例如,浮點(diǎn)數(shù)可以被格式化為一般格式或用冪來表示。 'b' - 二進(jìn)制。將數(shù)字以2為基數(shù)進(jìn)行輸出。 'c' - 字符。在打印之前將整數(shù)轉(zhuǎn)換成對(duì)應(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' - 冪符號(hào)。用科學(xué)計(jì)數(shù)法打印數(shù)字。用'e'表示冪。 'g' - 一般格式。將數(shù)值以fixed-point格式輸出。當(dāng)數(shù)值特別大的時(shí)候,用冪形式打印。 'n' - 數(shù)字。當(dāng)值為整數(shù)時(shí)和'd'相同,值為浮點(diǎn)數(shù)時(shí)和'g'相同。不同的是它會(huì)根據(jù)區(qū)域設(shè)置插入數(shù)字分隔符。 '%' - 百分?jǐn)?shù)。將數(shù)值乘以100然后以fixed-point('f')格式打印,值后面會(huì)有一個(gè)百分號(hào)。 '''print '6:\t|{0:b}'.format(3) print '7:\t|{0:c}'.format(3) print '8:\t|{0:d}'.format(3) print '9:\t|{0:o}'.format(3) print '10:\t|{0:x}'.format(3) print '11:\t|{0:e}'.format(3.75) print '12:\t|{0:g}'.format(3.75) print '13:\t|{0:n}'.format(3.75) #浮點(diǎn)數(shù) print '14:\t|{0:n}'.format(3) #整數(shù) print '15:\t|{0:%}'.format(3.75)#輸入形式的控制format a = int(raw_input('a:')) b = int(raw_input('b:')) print '16:\t|%*.*f' % (a, b, 1.1415926)print '17:\t|{array[2]}'.format(array=range(10)) print '18:\t|{attr.__class__}'.format(attr=0) print '19:\t|{digit:*^ 10.5f}'.format(digit=1.0/3)''' 類和類型可以定義一個(gè)__format__()方法來控制怎樣格式化自己。 它會(huì)接受一個(gè)格式化指示符作為參數(shù): ''' def __format__(self, format_spec):if isinstance(format_spec, unicode):return unicode(str(self))else:return str(self)總結(jié)
以上是生活随笔為你收集整理的python输出格式化及函数format的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python获取系统硬件信息
- 下一篇: python安装pyquery失败