print()
print默認是換行的,是end='\n'在起作用,
要想不換行你應該寫成 print(str, end = '')
>>> print('The length of %s is %d.' %('Python', len('Python')))
The length of Python is 6.
看看《Python基礎編程》中對格式化輸出的總結:
(1). %字符:標記轉換說明符的開始
(2). 轉換標志:-表示左對齊;+表示在轉換值之前要加上正負號;“”(空白字符)表示正數之前保留空格;0表示轉換值若位數不夠則用0填充
(3). 最小欄位寬度:轉換后的字串至少應該具有該值指定的寬度,如果是,則寬度會從值元組中讀出,
(4). 點(.)后跟精度值:如果轉換的是實數,精度值就表示出現在小數點后的位數,如果轉換的是字串,那么該數字就表示最大欄位寬度,如果是,那么精度將從元組中讀出
>>> pi = 3.141592653
>>> print('%10.3f' % pi) #欄位寬10,精度3
3.142
>>> print("pi = %.*f" % (3,pi)) #用*從后面的元組中讀取欄位寬度或精度
pi = 3.142
>>> print('%010.3f' % pi) #用0填充空白
000003.142
>>> print('%-10.3f' % pi) #左對齊
3.142
>>> print('%+f' % pi) #顯示正負號
+3.141593
print()與format()一起輸出:
print('b - 二進制,將數字以2為基數進行輸出: {0:b}'.format(50))
print('c - 字符,在列印之前將整數轉換成對應的Unicode字串: {0:c}'.format(50))
print('d - 十進制整數,將數字以10為基數進行輸出: {0:d}'.format(50))
print('o - 八進制,將數字以8為基數進行輸出: {0:o}'.format(50))
print('x - 十六進制,將數字以16為基數進行輸出,9以上的位數用小寫字母: {0:x}'.format(50))
print('e - 冪符號,用科學計數法列印數字,用e表示冪: {0:e}'.format(50))
print('g - 一般格式,將數值以fixed-point格式輸出,當數值特別大的時候,用冪形式列印: {0:g}'.format(50))
print('n - 數字,當值為整數時和d相同,值為浮點數時和g相同,不同的是它會根據區域設定插入數字分隔符: {0:n}'.format(50.12))
print('% - 百分數,將數值乘以100然后以fixed-point(f)格式列印,值后面會有一個百分號: {0:%}'.format(50.12))
# 輸出如下:
b - 二進制,將數字以2為基數進行輸出: 110010
c - 字符,在列印之前將整數轉換成對應的Unicode字串: 2
d - 十進制整數,將數字以10為基數進行輸出: 50
o - 八進制,將數字以8為基數進行輸出: 62
x - 十六進制,將數字以16為基數進行輸出,9以上的位數用小寫字母: 32
e - 冪符號,用科學計數法列印數字,用e表示冪: 5.000000e+01
g - 一般格式,將數值以fixed-point格式輸出,當數值特別大的時候,用冪形式列印: 50
n - 數字,當值為整數時和d相同,值為浮點數時和g相同,不同的是它會根據區域設定插入數字分隔符: 50.12
% - 百分數,將數值乘以100然后以fixed-point(f)格式列印,值后面會有一個百分號: 5012.000000%
format()
>>> print('The name is {}, age is {}'.format('James', 18))
The name is James, age is 18
>>> print('The name is {0}, age is {1}'.format('James', 18))
The name is James, age is 18
>>> print('The name is {1}, age is {0}'.format('James', 18))
The name is 18, age is James
>>> print('The name is {name}, age is {age}'.format(name='James', age=18))
The name is James, age is 18
>>> print('The name is {0}, age is {1}, fav is {other}'.format('James', 18, other='ball'))
The name is James, age is 18, fav is ball
>>>
>>> import math
>>> print('The value of PI is {}'.format(math.pi))
The value of PI is 3.141592653589793
>>> print('The value of PI is {!a}'.format(math.pi))
The value of PI is 3.141592653589793
>>> print('The value of PI is {!s}'.format(math.pi))
The value of PI is 3.141592653589793
>>> print('The value of PI is {!r}'.format(math.pi))
The value of PI is 3.141592653589793
>>> print('The value of PI is {0:.2f}'.format(math.pi))
The value of PI is 3.14
>>>
>>> table = {'chinese':88, 'math':96, 'english':99}
>>> for x, y in table.items():
print('{0:10} => {1:10d}'.format(x, y))
chinese => 88
math => 96
english => 99
>>>
>>> for x, y in table.items():
print('{0:>10} => {1:<10d}'.format(x, y)) # >右對齊, <(默認)左對齊
chinese => 88
math => 96
english => 99
>>>
>>> for x, y in table.items():
print('{0:^10} => {1:=10d}'.format(x, y)) # ^中間對齊
chinese => 88
math => 96
english => 99
>>> table = {'chinese':88, 'math':96, 'english':99}
>>> print('chinese:{0[chinese]:d}, math:{0[math]:d}, englist:{0[english]:d}'.format(table))
chinese:88, math:96, englist:99
>>>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/224927.html
標籤:Python
