跟老齊學Python之從格式化表達式到方法
現在我們就格式化方法做一個詳細一點的交代。
基本的操作
所謂格式化方法,就是可以先建立一個輸出字符串的模板,然后用format來填充模板的內容。
>>> #先做一個字符串模板
>>> template = "My name is {0}. My website is {1}. I am writing {2}."
>>> #用format依次對應模板中的序號內容
>>> template.format("qiwsir","qiwsir.github.io","python")
'My name is qiwsir. My website is qiwsir.github.io. I am writing python.'
當然,上面的操作如果你要這樣做,也是可以的:
>>> "My name is {0}. My website is {1}. I am writing {2}.".format("qiwsir","qiwsir.github.io","python")
'My name is qiwsir. My website is qiwsir.github.io. I am writing python.'
這些,跟用%寫的表達式沒有什么太大的區(qū)別。不過看官別著急,一般小孩子都區(qū)別不到,長大了才有區(qū)別的。慢慢看,慢慢實驗。
除了可以按照對應順序(類似占位符了)填充模板中的位置之外,還能這樣,用關鍵字來指明所應該田中的內容。
>>> template = "My name is {name}. My website is {site}"
>>> template.format(site='qiwsir.github.io', name='qiwsir')
'My name is qiwsir. My website is qiwsir.github.io'
關鍵詞所指定的內容,也不一定非是str,其它的數據類型也可以。此外,關鍵詞和前面的位置編號,還可以混用。比如:
>>> "{number} is in {all}. {0} are my number.".format("seven",number=7,all=[1,2,3,4,5,6,7,8,9,0])
'7 is in [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]. seven are my number.'
是不是開始感覺有點意思了?看輸出結果,就知道,經過format方法得到是一個新的str。
序列對象的偏移量
有這樣一個要求:在輸出中,顯示出一個單詞的第一個字母和第三個個字母。比如單詞python,要告訴看官,第一字母是p,第三個字母是t。
這個問題并不難。實現方法也不少,這里主要是要展示一下偏移量在format中的應用。
>>> template = "First={0[0]}, Third={0[2]}"
>>> template.format(word)
'First=p, Third=t'
list也是序列類型的,其偏移量也可。
>>> word_lst = list(word)
>>> word_lst
['p', 'y', 't', 'h', 'o', 'n']
>>> template
'First={0[0]}, Third={0[2]}'
>>> template.format(word_lst)
'First=p, Third=t'
對上面的綜合一下,稍微啰嗦一點的實驗:
>>> template = "The word is {0}, Its first is {0[0]}. Another word is {1}, Its second is {1[1]}."
>>> template.format("python","learn")
'The word is python, Its first is p. Another word is learn, Its second is e.'
>>> "{name}\'s first is {name[0]}".format(name="qiwsir") #指定關鍵詞的值的偏移量
"qiwsir's first is q"
值得注意的是,偏移量在序列類型的數據中,因為可以是負數,即能夠從右邊開始計數。
>>> word
'python'
>>> word[-1]
'n'
>>> word[-2]
'o'
但是,在模板中,無法使用負數的偏移量。
>>> "First={0[0]}, End={0[-1]}".format(word) #報錯
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string indices must be integers, not str
>>> "First={0[0]}, End={0[5]}".format(word) #把-1改為5就可以了。
'First=p, End=n'
當然,放到模板外面是完全可行的。這樣就好了:
>>> "First={0}, End={1}".format(word[0],word[-1])
'First=p, End=n'
dictionary的鍵
直接上實驗,先觀察,再得結論
>>> myinfo
{'website': 'qiwsir.github.io', 'name': 'qiwsir', 'room': 703}
>>> template = "I am {0[name]}"
>>> template.format(myinfo)
'I am qiwsir'
>>> template = "I am {0[name]}. My QQ is {qq}"
>>> template.format(myinfo,qq="26066913")
'I am qiwsir. My QQ is 26066913'
位置后面跟鍵,就能得到format的參數中字典的鍵對應的值。太羅嗦了吧,看例子就明白了。出了根據位置得到,還能夠根據關鍵詞得到:
>>> myinfo
{'website': 'qiwsir.github.io', 'name': 'qiwsir', 'room': 703}
>>> "my website is {info[website]}, and I like {0}".format("python",info=myinfo) #關鍵詞info引用的是一個字典
'my website is qiwsir.github.io, and I like python'
模板中添加屬性
看標題不懂在說什么。那就看實驗吧。
>>> import math
>>> "PI is {PI.pi}".format(PI=math)
'PI is 3.14159265359'
這是用關鍵詞,下面換個稍微復雜點,用位置的。
>>> import sys,math
>>> 'PI is {0.pi}. My lptop runs {1.platform}'.format(math,sys)
'PI is 3.14159265359. My lptop runs linux2'
看官理解了吧。
其它進制
在這個世界上的數學領域,除了有我們常常用到的十進制、十二進制(幾點了,這是你我常用到的,鐘表面就是12進制)、六十進制(這個你也熟悉的)外,還有別的進制,比如二進制、八進制、十六進制等等。此處不談進制問題,有興趣詳細了解,請各自google。不過,進制的確在計算機上很重要的。因為機器在最底層是用二進制的。
這里只是說明一下輸出時候的進制問題。
>>> "{0:X}, {1:o}, {2:b}".format(255,255,255)
'FF, 377, 11111111'
X:十六進制,Hex
o:八進制,octal
b:二進制,binary
順便補充,對于數的格式化方法輸出和格式化表達式一樣,就不贅述了。
在格式化方法中,還能夠指定字符寬度,左右對齊等簡單排版格式,不過,在我的經驗中,這些似乎用的不怎么多。如果看官需要,可以google或者到官方文檔看看即可。
關于格式化表達式和格式化方法,有的人進行了不少比較,有的人說用這個,有的人傾向用那個。我的建議是,你用哪個順手就用哪個。切忌門派之見呀。不過,有人傳說格式化表達式可能在將來某個版本中廢除。那是將來的事情,將來再說好了?,F在,你就撿著順手的用吧。
相關文章
pandas.DataFrame 根據條件新建列并賦值的方法
下面小編就為大家分享一篇pandas.DataFrame 根據條件新建列并賦值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python使用random和tertools模塊解一些經典概率問題
這篇文章主要介紹了Python使用random和tertools模塊解一些經典概率問題,本文講解了使用random和tertools模塊解羊車門問題、撲克牌問題、生日悖論等經典概率問題,需要的朋友可以參考下2015-01-01

