Python第五周学习笔记

Python第五周学习笔记

前言

发现之前并没有字符串相关的介绍,再者此次做的有点多,就整理一下发出来算了。此处的例子将代码和运行结果写在了一起,并未分开,细心看下也是可以看懂的,一般不存在 >>>的行均为结果。

正文

长度(len()函数)

例:

>>> a = 'Michael'
>>> len(a)
7

例2:

>>> a="hello woird"
>>> range(len(a))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

拼接(+)

只可拼接字符/串,不可拼接整数 例:

>>> name = a +' Jordan'
>>> print name
Michael Jordan

重复(*)

必须×整数 例:

>>> name * 3
'Michael JordanMichael JordanMichael Jordan'

成员运算符(in)

判断一个字符串是否是另一个字符串的字串,区分大小写 返回值:True或False 例:

>>> 'a' in name
True
>>> 'jordan' in name
False
>>> 'Jordan' in name
True

for语句

枚举字符串的每个字符 例:

for>>> for c in name:
print c
M
i
c
h
a
e
l
J
o
r
d
a
n

 字符串索引(index)

字符串中的每个字符都有一个索引值(下标) 索引从0(向前)或-1(向后)开始

>>> s = 'Hello World'
>>> s[-1]
'd'
>>> s[2]
'l'
>>> s[-2]
'l'
>>> s[-3]
'r'

[:]切片

选择字符串的子序列

1.[star]

star:子序列开始位置的索引值 finish:子序列结束位置的下一个字符的索引值(即不包含finish位置的值) 例:

>>> s = 'Hello World'
>>> s[6:10]
'Worl'
>>> s[6:]
'World'
>>> s[:6]
'Hello '
#两个都不写为整个字符串
>>> s[:]
'Hello World'
#finish可超出字符串范围,此时不提时越界,而是从字符串开始到结尾
>>> s[:20]
'Hello World' <br>

2.接受三个参数[start:finish]

计数参数<默认countby为1> 例:

>>> s = 'Hello World'
>>> s[4:10:2]
'oWr' <br>

字符串是不可改变的

一旦生成,内容不能改变 例:

>>> s = 'Hello World'
>>> s[1] = 'a'

结果报如下错误: [callout class=“danger” title=""] Traceback (most recent call last): File “<pyshell#25>”, line 1, in s[1] = ‘a’ TypeError: ‘str’ object does not support item assignment [/callout]

通过切片等操作,生成一个新的字符串

例:

>>> d =s
>>> d = d[:1]+'a'+s[2:]
>>> d
'Hallo World'
>>> d[:1]+'o'+s[2:]
'Hollo World'
>>> d
'Hallo World'

字符串方法

replace(old,new)

生成一个新的字符串,其中使用new替换old子串 依旧无法修改原字符串,若想修改需要将其重新赋值给原字符串

find()

查询其字串,返回第一次出现的时的下标。 例:

>>> d = 'Hello World'
>>> d.find("l")
2

split()

将原字符串切分成若干字符串,若任何参数,则在空格处切分。 例:

>>> s = 'hello world'
>>> s.split()
['hello', 'world']

str.strip()

去开始的空格回车等字符 例:

>>> s=" Hello word"
>>> s
' Hello word'
>>> s.strip()
'Hello word'

str.title()

首字母大写,其余字母小写 例:

>>> s = 'hello world'
>>> s.title()
'Hello World'

str.isdigit()

判断这个字符是否是数字字符 即’0’~‘9’ 例:

>>> s = 'hello world'
>>> s.isdigit()
False

s.ljust(width,[fillchar])

s.ljust(width,[fillchar]) 左对齐输出15个字符,不足部分用’.‘补足,默认用空格补足 例:

>>> s.ljust(15,'.')
'hello world....'

s.rjust(width,[fillchar])

s.rjust(width,[fillchar]) 右对齐输出15个字符,不足部分用’[fillchar]‘补足,默认用空格补足 例:

>>> s.rjust(15)
' hello world'

 s.count([fillchar])

获取字符串中第一个[fillchar]的下标 例:

>>> s = 'hello world'
>>> s.count('l')
3

字符串比较

字典序: [callout class=“danger” title=""] 首先比较两个字符串的第一个字符 若相同,则比较下一个字符 若不同,则字符串的大小关系由这两个字符的关系决定 若其中一个字符为空(较短),则其更小 [/callout]

字符串格式化

format方法

>>> print 'hello {} good {}.'.format(5,'DAY')
hello 5 good DAY.

括号的格式 [callout class=“danger” title=""] {field name width.precision type} {域的名字<对齐方式> 占用的宽度.精度 类型} >(大于号)右对齐 <(小于号)左对齐 [/callout] 例:

>>> import math
>>> "Pi is {}".format(math.pi)
'Pi is 3.14159265359'
>>> 'PI is {:.4f}'.format(math.pi)
'PI is 3.1416'
>>> 'PI is {:.4e}'.format(math.pi)
'PI is 3.1416e+00'
>>> 'PI is {:9.4f}'.format(math.pi)
'PI is 3.1416'
>>> 'PI is {:<9.4f}'.format(math.pi)
'PI is 3.1416 '

文件操作步骤

1.打开文件

f= open(filename,mode)

mode有r(读,默认),w(写)等

2.按行读取文件内容

for line in f:
pass

3.关闭文件

f.close()

写文件

f.write(str)

 

正则表达式

用来描述字符串的模式: .表示任意字符 d+表示一系列数字 [a-z]表示一个小写字母

待写专项

正则表达式 字符串处理函数

本文采用 CC BY-NC-SA 4.0 许可,转载请注明出处与作者。

支持一下

如果这篇文章对你有帮助,欢迎打赏或分享。

打赏支持

评论