5.1 字符串扩展使用

5.1.1 字符串索引(下标)

在Python中,字符串可以像其它语言的数组一样进行索引使用,方法是:字符串变量名[索引]

索引是从0开始,最大的索引值是字符串长度-1,如果使用了超出最大值,将会出现索引错误。

>>> myStr = 'abcdefg'
>>> myStr[0]
'a'
>>> myStr[6]
'g'
>>> myStr[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

正向的索引也就是从字符串开始向尾部的方向,使用的数值逐渐增加。Python还支持负数的索引值,方向是从字符串尾部向字符串开始的方向,同样超出了索引界限,会出现索引错误:5.1-下标.py

# 下标:给字符串里面每一个字符定义的编号,可以通过下标访问到每一个字符。
# 下标从0开始,最大值是字符串总长度-1
# 访问方式:[下标]
mystr = 'abcdefg'
print('完整输出:', mystr)
print(mystr[0])
print(mystr[5])

# 下标可以为负,从字符串最后一个字符到字符串最开头的一个字符
# 从-1开始,直到-字符串总长度
print(mystr[-1])
print(mystr[-5])

# 下标越界访问会产生错误,不论正负
# print(mystr[7])
# print(mystr[-8])

运行结果如下:

完整输出: abcdefg
a
f
g
c

索引的对应关系见下表:

myStr a b c d e f g
正向 0 1 2 3 4 5 6
负向 -7 -6 -5 -4 -3 -2 -1

5.1.2 字符串函数

Python提供了很多用于字符串处理的函数,可以实现对字符串的各种处理。字符串是不可变数据类型,所以不能对字符串进行任何的修改。

参见:5.2-字符串操作1.py,5.3-字符串操作2.py

<1> find,rfind

查找字符串,严格区分大小写,返回值为字符串第一次出现所在的索引,后面的不管;未找到字符串,返回-1。

rfind从右边开始查找。

mystr.find(str, start=0, end=len(mystr))

例如:

>>> mystr = 'Hello World zhangsan and world'
>>> print('012345678901234567890123456789')
012345678901234567890123456789
>>> print(mystr)
Hello World zhangsan and world
>>> print(mystr.find('world'))
25
>>> print(mystr.find('Man'))
-1
>>> print(mystr.find('orld'))
7

<2> index,rindex

基本等同于find,区别:如果index查找不到,给出异常错误。

rindex从右边开始查找。

>>> print(mystr.index('Man'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

<3> startswith,endswith

返回是否以字符串开头或结尾。返回值为bool类型。

>>> print(mystr.startswith('He'))
True
>>> print(mystr.endswith('ld'))
True

<4> isalpha,isdigit,isalnum,isspace

判断字符串是否全是字母,数字,字母或数字,空格。如果是则返回True,否则返回False。注意空字符串在isspace判断的结果是False,因为空格是一个字符,而空字符串没有字符。

>>> print(mystr.isalpha())
False
>>> print('1234'.isdigit())
True
>>> print('abcd1234'.isalnum())
True
>>> print('  '.isspace())
True
>>> print(''.isspace())
False

<5> count

统计字符串出现次数。

>>> print(mystr.count('orld'))
2

<6> replace

替换,第三个参数为替换的最大次数。

>>> print(mystr.replace('d', '==@=='))
Hello Worl==@== zhangsan an==@== worl==@==
>>> print(mystr.replace('d', '==@==', 2))
Hello Worl==@== zhangsan an==@== world

<7> capitalize,title,lower,upper

capitalize将字符串中的第一个字符大写;

title将字符串的每个单词首字母大写;

lower将全部字符改为小写;

upper将全部字符改为大写:

>>> print('abcd efgh'.capitalize())
Abcd efgh
>>> print('abcd efgh'.title())
Abcd Efgh
>>> print(mystr.lower())
hello world zhangsan and world
>>> print(mystr.upper())
HELLO WORLD ZHANGSAN AND WORLD

<8> join

使用字符串来连接join函数中参数的每一个元素,构造出一个新的字符串:

>>> print('_'.join(mystr))
H_e_l_l_o_ _W_o_r_l_d_ _z_h_a_n_g_s_a_n_ _a_n_d_ _w_o_r_l_d

<9> split,splitlines

用给定的字符串切分字符串。输出的结果是一个列表。splitlines按照行分隔,返回一个各行的列表。

>>> print(mystr.split(' '))
['Hello', 'World', 'zhangsan', 'and', 'world']
>>> print(mystr.split('l'))
['He', '', 'o Wor', 'd zhangsan and wor', 'd']

>>> mystr1 = 'hello\nworld\nOK'
>>> print(mystr1)
hello
world
OK
>>> print(mystr1.splitlines())
['hello', 'world', 'OK']

<10> partition,rpartition

将字符串分成三部分,注意如果切分的字符串没有的话,仍然是三部分。

rpartition 从右边开始分解。

>>> print(mystr.partition('zhangsan'))
('Hello World ', 'zhangsan', ' and world')
>>> print(mystr.partition('IT'))
('Hello World zhangsan and world', '', '')

<11> ljust,rjust,center

使字符串在给定的空间里左对齐,右对齐,居中对齐:

>>> print(mystr.ljust(60))
Hello World zhangsan and world
>>> print(mystr.rjust(60))
                              Hello World zhangsan and world
>>> print(mystr.center(60))
               Hello World zhangsan and world

<12> lstrip,rstrip,strip

去掉字符串左边,右边和两边的空格或给定的字符串,不会去掉字符串中间的空格或字符串:

>>> str1 = '    hello world   '
>>> print(str1.lstrip())
hello world
>>> print(str1.rstrip())
    hello world
>>> print(str1.strip())
hello world