Python字符串顯示實際應用技巧分享
Python編程語言的應用,對于開發人員來說是一個比較好掌握的計算機程序語言。在實際應用中,字符串的應用是一個比較基礎的操作技術。我們今天就為大家詳細介紹一下有關Python字符串顯示的相關操作。
可能是我還沒找到更好的方法,但是小遺憾的是,Python似乎目前無法支持類似shell腳本,perl所支持的標量內插,所以在顯示的時候無法像下面,這種個人感覺,最清晰,方便的方法。
比如,用戶輸人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字符串顯示如何實現呢,有如下方法,按需選擇吧,希望以后能直接支持類似sell腳本的顯示方法,把$看作轉義符:)
- 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)
以上就是我們為大家介紹的有關Python字符串顯示的相關內容。
【編輯推薦】