Python字符串显示实际应用技巧分享 - Go语言中文社区

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字符串显示的相关内容。

版权声明:本文来源51CTO,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:http://developer.51cto.com/art/201003/186120.htm
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-16 17:07:50
  • 阅读 ( 919 )
  • 分类:Go应用

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢