(1)格式化輸出的方式有很多種,通常需要格式化輸出的都是字符串,簡單的可以通過+號或者join函數來處理
(2) linux系統中使用較多的是C風格的輸出,用熟了也比較方便
(3)在python3中,主要以format函數來完成字符串的格式化處理
+號與join函數
加號+:
對兩個字符串進行連接
join函數:
可以對一些離散的或者有序的序列進行連接處理,可以自定義連接的間隔符
#join函數是字符串的方法
'sep'.join(str)
>>> str1='abc'
>>> str2='def'
>>> print(str1+'+'+str2+'='+str1+str2)
abc+def=abcdef
>>> x=list('abcdefg')
>>> ','.join(x)
'a,b,c,d,e,f,g'
>>> sep=':' #分隔符也可以用變量來代替
>>> sep.join(x)
'a:b:c:d:e:f:g'
>>> import random
>>> x='abcdefghijklmnopqrstuvwxyz'
>>> [''.join(random.sample(x,2)) for _ in range(10)] #也可以使用表達式生成器
['bi', 'we', 'fn', 'oj', 'wx', 'fe', 'bs', 'wd', 'ko', 'by']
C風格的格式化輸出
C風格的輸出方式在linux系統中應用較多,主要以%號來標識,輸出函數使用printf
>>> person=dict(name='xiejiawen',age=25,sex='male',country='China') #此處先定義一個字典,字典其實就是一個個的鍵值對
>>> print('My name is %s' % person['name']) #通過鍵來訪問字典中的元素,注意key是字符串,因此需要使用引號包圍
>>> print('my country is %s' % person['country'])
>>> print("I'm %s years old" % person['age'])
My name is xiejiawen
my country is China
I'm 25 years old
>>> print("%(host)s.%(domain)s" % {'domain':'magedu.com', 'host':'www'}) # 靠名字對應
www.magedu.com
對齊
%+s:在%後面添加+號即可右對齊
%-s:在%後面天加-號即可左對齊
不添加這兩個字符時,默認爲右對齊方式
>>> print('%09d' % 5) #字符寬度設置爲9,右對齊,不夠的以0填充
>>> print('%-5d' % 20)
000000005
20 ##20後面其實還有三個空字符
#如果不要減號
print('%09d' % 5)
print('%5d' % 20)
000000005
20 #右對齊20前面有三個空字符
format函數
#expression段用於描述輸出格式,後續的str1等字符串用於填充expression段中的空白部分
expression.format(str1[,str2])
位置對應
>>> print('{}-{}'.format('hangzhou','China') + ' is a first_line city') #默認情況下,後面字符串的順序和前面{}的順序一一對應
hangzhou-China is a first_line city
>>> print('{1}-{0}'.format('China','hangzhou') + ' is a first_line city') #也可以用索引來指定後續的字符串
hangzhou-China is a first_line city
位置或關鍵字對應
>>> print("{server} :{1}:{0}".format('8080','192.168.99.100',server='www.baidu.com'))
www.baidu.com :192.168.99.100:8080
訪問元素
>>> print('{0[0]} {0[1]}'.format(('hello','world'))) #使用元組定義,然後使用索引來訪問。
>>> print('{} {}'.format('hello' , 'world')) #此方式更爲方便
hello world
浮點數
# 浮點數
>>> print("{}".format(3**0.5)) # 開方運算
>>> print("{:f}".format(3**0.5)) # 1.732051,精度默認6
>>> print("{:10f}".format(3**0.5)) # 右對齊,寬度10
>>> print("{:2}".format(102.231)) # 寬度爲2數字,不過數字的有效位不止2位,此限制無效
>>> print("{:2}".format(1)) # 寬度爲2數字,前面添加空格
>>> print("{:.2}".format(3**0.5)) # 1.7 2個數字
>>> print("{:.2f}".format(3**0.5)) # 1.73 小數點後2位
>>> print("{:3.2f}".format(3**0.5)) # 1.73 寬度爲3,小數點後2位
>>> print("{:20.3f}".format(0.2745)) # 0.275數字寬度爲20位,不夠的前面添加空格
>>> print("{:3.3%}".format(1/3)) # 33.333%,由於小數點後就佔了三位,因此3位的數字限制無效
1.7320508075688772
1.732051
1.732051
102.231
1
1.7
1.73
1.73
0.275
33.333%
對齊
print("{}*{}={}".format(5, 6, 5*6))
print("{}*{}={:2}".format(5, 6, 5*6)) #{:2}表示寬度爲兩個字符
print("{1}*{0}={2:3}".format(5, 6, 5*6)) #{2:3}表示填入第三個元素,並且寬度爲3,默認右對齊
print("{1}*{0}={2:0>3}".format(5, 6, 5*6)) #{2:0>3}表示右對齊並且佔用3個字符寬度
print("{}*{}={:#<3}".format(4, 5, 20)) #{:#<3}左對齊佔用3個字符不足的地方使用#號填充
print("{:#^7}".format('*' * 3)) #居中對齊佔7個字符寬度,不足的地方兩端使用#號填充
bonus
#打印正三角與倒三角
def output_num(n):
for i in range(1,n+1):
for _ in range(n-i+1):
print('{0:3}'.format(' '),end='')
for k in range(i,0,-1):
print('{0:3}'.format(k),end='')
print()
print()
for i in range(1,n+1):
for k in range(i):
print('{0:3}'.format(' '),end='')
for j in range(n-i+1,0,-1):
print('{0:3}'.format(j),end='')
print()
n=int(input('n='))
output_num(n)
n=12
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
12 11 10 9 8 7 6 5 4 3 2 1
12 11 10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
#打印等腰三角形
def cout(n):
for i in range(1,n+1):
for j in range(n-i+1,1,-1): #每一行循環先打印空格
print('%3s' % ' ',end='')
for k in range(i,0,-1): #每一行循環再打印*號
print('%3d' % k,end='')
for a in range(2,i+1):
print('%3d' % a,end='')
print() #結束當前行的循環
n=int(input('n='))
cout(n)
n=12
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
#感興趣的可以琢磨一下怎麼打印倒等腰三角形
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
7 6 5 4 3 2 1 2 3 4 5 6 7
6 5 4 3 2 1 2 3 4 5 6
5 4 3 2 1 2 3 4 5
4 3 2 1 2 3 4
3 2 1 2 3
2 1 2
1