Python字符串顯示實際應(yīng)用技巧分享
Python編程語言的應(yīng)用,對于開發(fā)人員來說是一個比較好掌握的計算機程序語言。在實際應(yīng)用中,字符串的應(yīng)用是一個比較基礎(chǔ)的操作技術(shù)。我們今天就為大家詳細(xì)介紹一下有關(guān)Python字符串顯示的相關(guān)操作。
可能是我還沒找到更好的方法,但是小遺憾的是,Python似乎目前無法支持類似shell腳本,perl所支持的標(biāo)量內(nèi)插,所以在顯示的時候無法像下面,這種個人感覺,最清晰,方便的方法。
比如,用戶輸人2個變量‘basketball', 'swimming', shell或者per可以如下顯示出 I love basketball and swimming the best.
- #shell
- input = 'basketball'
- input2 = 'swimming'
- print 'I love $input and $input2 the best'
- #perl
- $input = 'basketball'
- $input2 = 'swimming'
- print 'I love $input and $input2 the best'
Python字符串顯示如何實現(xiàn)呢,有如下方法,按需選擇吧,希望以后能直接支持類似sell腳本的顯示方法,把$看作轉(zhuǎn)義符:)
- import sys
- input = sys.argv[1]
- input2 = sys.argv[2]
- s = 'I love ' + input + ' and ' + input2 + ' the best'
- print(s)
- s = 'I love %s and %s the best'%(input, input2)
- print(s)
- params= {'input': input, 'input2': input2 }
- s = 'I love %(input)s and %(input2)s the best'%params
- 13 print(s)
- from string import Template
- s = Template('I love $input and $input2 the best')
- ss =s.substitute(inputinput=input, input2input2=input2)
- print(s)
以上就是我們?yōu)榇蠹医榻B的有關(guān)Python字符串顯示的相關(guān)內(nèi)容。
【編輯推薦】