Python字符串显示实际应用技巧分享


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

可能是我还没找到更好的方法,但是小遗憾的是,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字符串显示的相关内容。

相关内容

    暂无相关文章

评论关闭