Python中的字符串怎么显示

本篇内容介绍了“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中的字符串怎么显示”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!