------------------------------------------- ------------------------------------------- 字體色 | 背景色 | 顏色描述 ------------------------------------------- 30 | 40 | 黑色 31 | 41 | 紅色 32 | 42 | 綠色 33 | 43 | 黃色 34 | 44 | 藍色 35 | 45 | 紫紅色 36 | 46 | 青藍色 37 | 47 | 白色 ------------------------------------------- ------------------------------- 顯示方式 | 效果 ------------------------------- 0 | 終端默認設置 1 | 高亮顯示 4 | 使用下劃線 5 | 閃爍 7 | 反白顯示 8 | 不可見 -------------------------------
例:html
print('This is a \033[1;35m test \033[0m!') print('This is a \033[1;32;43m test \033[0m!') print('\033[1;33;44mThis is a test !\033[0m')
輸出結果:python
字符串格式化代碼:git
格式 | 描述 |
---|---|
%% | 百分號標記 |
%c | 字符及其ASCII碼 |
%s | 字符串 |
%d | 有符號整數(十進制) |
%u | 無符號整數(十進制) |
%o | 無符號整數(八進制) |
%x | 無符號整數(十六進制) |
%X | 無符號整數(十六進制大寫字符) |
%e | 浮點數字(科學計數法) |
%E | 浮點數字(科學計數法,用E代替e) |
%f | 浮點數字(用小數點符號) |
%g | 浮點數字(根據值的大小採用%e或%f) |
%G | 浮點數字(相似於%g) |
%p | 指針(用十六進制打印值的內存地址) |
%n | 存儲輸出字符的數量放進參數列表的下一個變量中 |
基本方法的使用:app
1 #!/usr/bin/python 2 #coding=utf-8 3 ''' 4 能夠指定所需長度的字符串的對齊方式: 5 < (默認)左對齊 6 > 右對齊 7 ^ 中間對齊 8 = (只用於數字)在小數點後進行補齊 9 ''' 10 print ('1:\t|{0:>10},'.format('wangyu')) 11 print ('2:\t|{0:4.2f}'.format(1.1415926)) 12 print ('3:\t|',format(1.1415926,'<10.2f')) 13 print ('4:\t|{0:<10},{1:<15}'.format('wangyu',1.1415926)) 14 print ('5:\t|User ID: {uid} Last seen: {last_login}'.format(uid='root',last_login = '5 Mar 2008 07:20') ) 15 16 '''格式化指示符能夠包含一個展現類型來控制格式。 17 例如,浮點數能夠被格式化爲通常格式或用冪來表示。 18 'b' - 二進制。將數字以2爲基數進行輸出。 19 'c' - 字符。在打印以前將整數轉換成對應的Unicode字符串。 20 'd' - 十進制整數。將數字以10爲基數進行輸出。 21 'o' - 八進制。將數字以8爲基數進行輸出。 22 'x' - 十六進制。將數字以16爲基數進行輸出,9以上的位數用小寫字母。 23 'e' - 冪符號。用科學計數法打印數字。用'e'表示冪。 24 'g' - 通常格式。將數值以fixed-point格式輸出。當數值特別大的時候,用冪形式打印。 25 'n' - 數字。當值爲整數時和'd'相同,值爲浮點數時和'g'相同。不一樣的是它會根據區域設置插入數字分隔符。 26 '%' - 百分數。將數值乘以100而後以fixed-point('f')格式打印,值後面會有一個百分號。 27 ''' 28 29 print ('6:\t|{0:b}'.format(3)) 30 print ('7:\t|{0:c}'.format(3)) 31 print ('8:\t|{0:d}'.format(3)) 32 print ('9:\t|{0:o}'.format(3)) 33 print ('10:\t|{0:x}'.format(3)) 34 print ('11:\t|{0:e}'.format(3.75)) 35 print ('12:\t|{0:g}'.format(3.75)) 36 print ('13:\t|{0:n}'.format(3.75))#浮點數 37 print ('14:\t|{0:n}'.format(3)) #整數 38 print ('15:\t|{0:%}'.format(3.75)) 39 40 #輸入形式的控制format 41 a = int(input('a:')) 42 b = int(input('b:')) 43 print ('16:\t|%*.*f' % (a, b, 1.1415926)) 44 45 print ('17:\t|{array[2]}'.format(array=range(10))) 46 print ('18:\t|{attr.__class__}'.format(attr=0)) 47 print ('19:\t|{digit:*^ 10.5f}'.format(digit=1.0/3)) 48 49 ''' 50 類和類型能夠定義一個__format__()方法來控制怎樣格式化本身。 51 它會接受一個格式化指示符做爲參數: 52 ''' 53 def __format__(self, format_spec): 54 if isinstance(format_spec, unicode): 55 return unicode(str(self)) 56 else: 57 return str(self)
#!/usr/bin/python #coding=utf-8 #使用str.format()函數 #使用'{}'佔位符 print('I\'m {},{}'.format('Hongten','Welcome to my space!')) print('#' * 40) #也能夠使用'{0}','{1}'形式的佔位符 print('{0},I\'m {1},my E-mail is {2}'.format('Hello','Hongten','hongtenzone@foxmail.com')) #能夠改變佔位符的位置 print('{1},I\'m {0},my E-mail is {2}'.format('Hongten','Hello','hongtenzone@foxmail.com')) print('#' * 40) #使用'{name}'形式的佔位符 print('Hi,{name},{message}'.format(name = 'Tom',message = 'How old are you?')) print('#' * 40) #混合使用'{0}','{name}'形式 print('{0},I\'m {1},{message}'.format('Hello','Hongten',message = 'This is a test message!')) print('#' * 40) #下面進行格式控制 import math print('The value of PI is approximately {}.'.format(math.pi)) print('The value of PI is approximately {!r}.'.format(math.pi)) print('The value of PI is approximately {0:.3f}.'.format(math.pi)) table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678} for name, phone in table.items(): print('{0:10} ==> {1:10d}'.format(name, phone)) table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; ''Dcab: {0[Dcab]:d}'.format(table))
本文內容來自:
https://www.cnblogs.com/RukawaKaede/p/6069977.html
https://www.cnblogs.com/ping-y/p/5897018.html函數