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

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

【编辑推荐】

  1. Python Socket编程实现网络编程
  2. Python base64模块基本概念总结
  3. Python取得文件列表基本应用方式浅谈
  4. Python抓取网页内容应用代码分析
  5. Python编码规范基本内容简介
责任编辑:曹凯 来源: 博客园
相关推荐

2009-12-11 13:16:04

PHP查询字符串

2010-05-21 17:22:22

2010-03-01 13:06:49

WCF继承

2009-09-01 17:50:23

C#截取字符串

2010-04-09 18:15:47

Oracle 字符串

2010-02-01 17:09:07

C++链表操作

2010-03-16 16:22:36

Python字符串

2010-03-05 16:09:44

Python中文字符

2010-03-01 17:52:03

WCF选择绑定

2010-03-16 10:58:35

Python字符串

2010-03-09 16:16:55

Python字符串

2009-12-08 16:33:45

PHP unpack函

2010-03-11 19:34:57

Python字符串

2010-03-03 16:57:28

Python字符

2010-03-22 18:53:53

Python格式化字符

2010-06-07 16:36:29

MySQL连接字符串

2010-04-15 16:47:46

Oracle字段

2023-08-21 10:28:00

字符串字符Python

2020-12-31 07:56:02

JavaScript 字符串技巧

2023-12-11 07:33:05

Go语言字符技巧
点赞
收藏

51CTO技术栈公众号