
1.4 字符串
字符串是由一个或一个以上字符组成的字符序列,是最常见的数据类型之一。
1.4.1 创建字符串
创建字符串,用单引号或双引号将字符序列包围起来赋值给变量即可。例如:

或者

如果字符串有换行,则用三引号包围它们。例如:

在返回结果中,\n为换行符。三引号是连续的三个单引号。
如果字符串中包含单引号或双引号,则可以用不同的引号将整个字符串包围起来进行赋值。例如:

注意:字符串创建以后,不能直接修改字符串中的字母或子字符串。例如:

可见,试图修改给定字符串中的第2个字符时返回一个出错信息,表示不能对字符串对象进行局部修改。
1.4.2 索引和切片
字符串的索引和切片,指的是从给定字符串中提取一个或多个单字符,或者部分连续的字符。在Python中使用“[]”对字符串进行索引和切片。
下面对给定字符串'abcdefg'进行索引,提取它的第2个字符和倒数第2个字符。

注意:从左到右索引时,基数为0;从右到左索引时,基数为-1。
切片操作是指从给定字符串中提取一个连续的子字符串。在Python中字符串切片操作的语法格式和说明如表1-4所示。
表1-4 字符串的切片操作

在正向操作时,遵循包头不包尾的原则,基数为0。例如:

索引号1对应的字符是"b",索引号4对应的字符是"e",结果为"bcd",打头的"b"被包括进来,结尾的"e"则没有被包括进来。
1.4.3 转义字符
在Python中用一些字符表示特殊的操作,比如用\n表示换行,用\r表示回车等。这些字符表达的不再是字符本身的意义,它们被称为“转义字符”。Python中常见的转义字符如表1-5所示。
表1-5 Python中常见的转义字符

1.4.1节在创建字符串时,如果字符串中包含单引号或双引号,则使用不同的引号来包围字符串。转义字符提供了另一种解决方法,例如:

如果希望转义字符保持它原始字符的含义,则在字符串前面添加“r”,指明不转义。例如:

在变量a引用的字符串中\n进行了转义,在变量b引用的字符串中\n指定不转义。所以在变量b返回的字符串中,“n”前面有两个斜杠,两个斜杠表示的是斜杠本身。
1.4.4 字符串的格式化输出
使用print函数输出字符串时,可以指定字符串的输出格式。其基本格式为:

其中,占位符用于表示该位置字符串的内容和格式。各占位符位置上字符串的内容按先后顺序取百分号后面圆括号里面的字符串。常见的字符串占位符如表1-6所示。
表1-6 常见的字符串占位符

下面结合示例说明字符串占位符的使用。

可以指定显示数字的符号、宽度和精度。下面指定按浮点数输出圆周率的值,数字宽度为10个字符,小数位数为5位,显示正号。

结果显示,小数点算1个字符,如果整个数字的宽度不足10个字符,则在数字前面用空格补齐。如果显示负号,则在数字末尾用空格补齐。不足位也可以用0补齐,例如:

除了使用占位符对字符串进行格式化,还可以使用format函数来实现。该函数用花括号{}标明被替换的字符串,与%占位符类似。使用format函数进行格式化时更灵活、更方便。
下面是使用format函数进行字符串格式化输出的一些例子。当{}中为空时,按先后顺序用format函数参数指定的字符串进行替换。

在{}中用整数指定占位符位置上显示什么字符串,该整数表示format函数参数指定的字符串出现的先后顺序,基数为0。

有重复的情况:

显示为浮点数并指定小数位数:

字符串显示为百分比格式,指定小数位数:

使用参数名称进行匹配:

1.4.5 字符串的长度和大小写
Python提供了一些返回字符串长度和转换字符串字母大小写的函数与方法,如表1-7所示。
表1-7 返回字符串长度和转换字符串字母大小写的函数与方法

下面是一些进行演示的例子。

使用input函数从控制台交互输入和读取字符串:

按回车键以后提示等待输入。在后面输入编号,将内容赋给id变量。

按回车键,在提示符后面输入id,按回车键后显示id的值。

1.4.6 字符串的分割、连接和删除
使用字符串的split方法,可以用指定字符作为分隔符,对给定字符串进行分割。例如,下面用逗号作为分隔符,对字符串"a,b,c"进行分割,结果以列表的形式给出(1.5节将介绍列表)。

默认时,split方法以空格作为分隔符进行分割。例如:

连接字符串可以使用+(加号)、*、空格和join方法等几种方法。下面使用“+”连接两个字符串。

使用“*”可以重复输出指定字符串。例如:

使用print函数,其参数用空格分隔几个字符串,空格能起到连接的作用。例如:

使用字符串的join方法,用指定字符或字符串分隔给定的多个字符串。例如,下面用逗号分隔给定字符串的各个字符。

或者用列表给出变量b引用的字符串,用变量a引用的字符串分隔列表各元素。例如:

使用字符串的strip方法可以去除字符串首尾指定的字符串,使用lstrip方法和rstrip方法可以去除字符串左侧和右侧指定的字符串。
下面去除给定字符串首尾的空格。

注意:中间的空格没有去除。也可以不指定参数,直接去除首尾全部空格。

下面使用lstrip方法和rstrip方法去除字符串左侧和右侧的空格。

使用del命令删除整个字符串。例如:

1.4.7 字符串的查找和替换
使用字符串的find方法和rfind方法可以返回一个字符串在另一个字符串中首次和最后出现的位置。
下面的代码返回字母a在字符串"abca"中首次和最后出现的位置。注意位置的基数为0。

使用字符串的count方法可以返回指定字符串在另一个字符串中出现的次数。例如,返回字母a在字符串"abca"中出现的次数。

使用字符串的startswith方法判断字符串是否以指定字符串开头,如果是,则返回True,否则返回False。例如:

使用字符串的endswith方法判断字符串是否以指定字符串结尾,如果是,则返回True,否则返回False。例如:

使用字符串的replace方法,用指定字符串替换给定字符串中的某个子字符串。该方法的语法格式为:

其中,str为给定的字符串,参数str1为给定字符串中被替换的子字符串,参数str2为用作替换的字符串,参数num指定替换不能超过的次数。
下面将给定字符串中的字母a替换为w,替换次数不能超过5次。

1.4.8 字符串的比较
在Python中,使用比较运算符、成员运算符以及与字符串相关的函数和方法进行字符串比较。
下面用“==”或“!=”比较两个字符串对象的值是否相等或不相等。

使用is比较两个字符串对象的内存地址是否相同。

使用成员运算符in或not in计算指定字符串是否包含或不包含在另一个字符串中,如果成立,则返回True,否则返回False。

Python提供的用于字符串比较的函数和方法如表1-8所示,可以用它们判断字符串中元素的类型和大小写等情况。
表1-8 用于字符串比较的函数和方法

下面举例说明。

1.4.9 字符串缓存机制
与整数缓存机制类似,Python为常用的字符串也提供了缓存机制。通过给常用的字符串提供缓存,可以避免频繁地分配内存和释放内存,避免内存中出现更多的内存碎片,从而提高Python的整体性能。
在命令行模式下,Python为只包含下画线、数字和字母的字符串提供缓存。在第一次创建满足要求的字符串对象时建立缓存,以后需要值相同的字符串时,可以直接从缓存池中取用,不用重新创建对象。
下面创建变量a和b,它们都引用值为"abc"的字符串,然后比较它们的值和地址。

按道理讲,变量a和b引用的是不同的对象,对象具有不同的地址,表达式a is b的返回值应该为False。但是因为Python提供了字符串缓存机制,并且字符串"abc"满足字符串中只包含下画线、数字和字母的要求,表达式a is b的返回值为True。即变量a和b引用的是同一个字符串对象,它在第一次创建后被放在缓存池中。
下面创建的变量a和b都引用值为"abc 123"的字符串,因为字符串中包含空格,不满足要求,所以不能为该字符串提供缓存。因此,表达式a is b的返回值为False,即变量a和b引用的是不同的字符串对象。
