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'
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
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)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
以上就是我们为大家介绍的有关Python字符串显示的相关内容。
【编辑推荐】