RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Python字符串显示实际应用技巧分享

Python编程语言的应用,对于开发人员来说是一个比较好掌握的计算机程序语言。在实际应用中,字符串的应用是一个比较基础的操作技术。我们今天就为大家详细介绍一下有关Python字符串显示的相关操作。

创新互联公司主要从事网站建设、成都网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务克什克腾,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

可能是我还没找到更好的方法,但是小遗憾的是,Python似乎目前无法支持类似shell脚本,perl所支持的标量内插,所以在显示的时候无法像下面,这种个人感觉,最清晰,方便的方法。
比如,用户输人2个变量‘basketball', 'swimming', shell或者per可以如下显示出 I love basketball and swimming the best.

 
 
 
  1. #shell
  2. input = 'basketball'
  3. input2 = 'swimming'
  4. print 'I love $input and $input2 the best'
  5. #perl
  6. $input = 'basketball'
  7. $input2 = 'swimming'
  8. print 'I love $input and $input2 the best'

Python字符串显示如何实现呢,有如下方法,按需选择吧,希望以后能直接支持类似sell脚本的显示方法,把$看作转义符:)

 
 
 
  1. import sys
  2. input = sys.argv[1]
  3. input2 = sys.argv[2]
  4. s = 'I love ' + input + ' and ' + input2 + ' the best'
  5. print(s)
  6. s = 'I love %s and %s the best'%(input, input2)
  7. print(s)
  8. params= {'input': input, 'input2': input2 }
  9. s = 'I love %(input)s and %(input2)s the best'%params
  10. 13 print(s)
  11. from string import Template
  12. s = Template('I love $input and $input2 the best')
  13. ss =s.substitute(inputinput=input, input2input2=input2)
  14. print(s)

以上就是我们为大家介绍的有关Python字符串显示的相关内容。


标题名称:Python字符串显示实际应用技巧分享
标题网址:http://www.jxjierui.cn/article/ccicjpe.html