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' 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

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) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

以上就是我们为大家介绍的有关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-02-01 17:09:07

C++链表操作

2010-04-09 18:15:47

Oracle 字符串

2010-03-01 17:52:03

WCF选择绑定

2010-03-05 16:09:44

Python中文字符

2024-09-06 17:32:55

字符串Python

2010-03-16 10:58:35

Python字符串

2010-03-16 16:22:36

Python字符串

2025-02-21 12:30:00

字符串前端JavaScript

2010-03-09 16:16:55

Python字符串

2010-03-03 16:57:28

Python字符

2009-12-08 16:33:45

PHP unpack函

2010-03-11 19:34:57

Python字符串

2009-12-15 10:23:23

Ruby应用技巧

2010-03-22 18:53:53

Python格式化字符

2009-12-18 09:52:40

Ruby字符串处理函数

2010-03-25 18:37:28

Python技巧
点赞
收藏

51CTO技术栈公众号