前言
在生活、工作中,python
一直都是一个好帮手。在
python
的众多功能中,我觉
得文本处理是最常用的。下面是平常使用中的一些总结。环境是
python 3.3
0. 基础
在
python
中,使用
str
对象来保存字符串。str
对象的建立很简单,使用单引
号或双引号或
3
个单引号即可。例如:
s=
'nice'
#output: nice
s=
"nice"
#output: nice
s=
"Let's go"
#output: Let's go
s=
'"nice"'
#output: "nice"
s=str(1)
#output: 1
s=
'''nice
day'''
#output: nice
#output: day
在
python
中,\n
代表换行符,\t
代表
tab
键
在
python
中,引用
str
中的某一段的内容很容易。例如:
s=
'123456789'
s[0]
#第一个字符: output: 1
s[-1]
#倒数第一个字符: output: 9
s[:2]
#前
2
个字符: output: 12
s[-2:]
#后
2
个字符: output: 89
s[2:-2]
#去掉前
2
个和后
2
个剩余的字符 output:34567
在
python
中,判断某一字符串是否在另一个字符串中:
'nice'
in
'nice day'
#output :True
task 1.