这节课我们来学习下 Python 基本数据类型中的字符串类型,字符串类型在实际的开发中是一个经常会用到的数据类型,比较重要。下面我们一起来看一下:
字符串类型的数据表示一段文本,使用单引号或者双引号创建:
在 python 中使用字符串的例子如下:
>>> x = 'hello'>>> x'hello'>>> y = "world">>> y'world'
使用单引号或者双引号创建的字符串只能在一行,而使用三引号允许一个跨多行的字符串。
使用 3 个单引号创建的多行字符串示例如下:
s = '''line 1
line 2
line 3'''print(s)
使用 3 个双引号创建的多行字符串示例如下:
s = """line 1
line 2
line 3"""print(s)
以上程序的输出如下:
line 1line 2line 3
本节介绍对字符的最常见的几种运算操作,包括:
使用运算符 + 连接两个字符串,示例如下:
>>> 'Hello' + 'World'>>> 'HelloWorld'>>> 'Hello' + ' ' + 'World'>>> 'Hello World'
使用运算符 * 重复输出字符串,示例如下:
>>> 'hello' * 2'hellohello'>>> 'hello' * 3'hellohellohello'
使用函数 len 获取字符串的长度,示例如下:
>>> len('abc')3>>> len('5axxw')5
通过索引 [] 获取字符串中指定位置的字符,示例如下:
>>> s = '5axxw'>>> s[0]'i'>>> s[1]'m'>>> s[2]'o'>>> s[3]'o'>>> s[4]'c'
在 Python 中,使用语法 string[start:end],获取字符串 string 中在 [start, end) 范围的子字符串。注意范围 [start, end) 包含 start,不包含 end。举例如下:
>>> s = '5axxw'>>> s[1]'m'>>> s[2]'o'>>> s[3]'o'>>> s[1:4]'moo'
通过关键字 in 检查字符串中是否包含指定字符串,示例如下:
>>> 'mooc' in '5axxw'True>>> 'mook' not in '5axxw'True
capitalize() 方法把字符串的第一个字符大写,示例如下:
>>> text = 'abc'>>> text.capitalize()'Abc'
count() 方法统计字符串出现的次数,示例如下:
>>> text = 'abc abc'>>> text.count('abc')2
startswith(target) 方法检查字符串是否是以字符串 target 开头,示例如下:
>>> text = 'abc'>>> text.startswith('ab')
True>>> text.startswith('bb')
False
endswith(target) 方法检查字符串是否是以字符串 target 结尾,示例如下:
>>> text = 'abc'>>> text.endsswith('bc')
True>>> text.endsswith('cc')
False
lower() 方法将字符串中所有大写字符转换为小写,示例如下:
>>> text = 'ABC'>>> text.lower()'abc'
upper() 方法将字符串中所有小写字符转换为大写,示例如下:
>>> text = 'abc'>>> text.upper()'ABC'
如果字符串中所有字符是小写则为真,否则为假,示例如下:
>>> text = 'abc'>>> text.islower()
True
如果字符串中所有字符是大写则为真,否则为假,示例如下:
>>> text = 'ABC'>>> text.isupper()
True
检查是否包含指定字符串 target,如果包含字符串 target 则返回开始的索引值,否则返回 -1,示例如下:
>>> text = '5axxw'>>> text.find('oo')2>>> text.find('oop')
-1
使用空格将字符串分割为多个单词,返回一个列表,示例如下:
>>> text = 'hello world'>>> text.split()
['hello', 'world']
缺省情况下,使用空格将字符串分割为多个单词,可以在 split() 方法中指定分隔符,示例如下:
>>> text = 'hello:world'>>> text.split(':')
['hello', 'world']
在 Python 中使用反斜杠 \ 加字母的组合来表示一些特殊字符,例如:\n 表示换行,\ 之后的字符 n 的含义已经不再是原来 ASCII 字符的含义了,所以被称为转义字符。
常用的转义字符如下所示:
转义字符 | 描述 |
---|---|
\t | 制表 |
\n | 换行 |
\’ | ’ |
\" | " |
\\ | \ |
>>> print('a\tb\tc')
a b c>>> print('aa\tbb\tcc')
aa bb cc
\t 表示跳到下一个制表符位置,可以使得输出纵向对齐。
>>> print('a\nb')
a
b
>>> print('\'')
'
在单引号字符串中使用 \’ 表示单引号
>>> print("\"")"
在双引号字符串中使用 \" 表示双引号
>>> print('\\')
\
在字符串中使用 ‘\’ 表示转义字符 \
通过使用字符串 + 连接运算可以将多个字符串拼接起来,例如:
>>> name = 'tom'>>> city = 'nanjing'>>> 'My name is ' + name + ', I live in ' + city + '.''My name is tom, I live in beijing.'
在第 3 行,使用字符串 + 将以下 5 个字符串拼接起来:
最终得到字符串 ‘My name is tom, I live in beijing’,像这样将多个字符串拼接起来的表达式非常不直观,Python 提供了字符串格式化用于简化拼接多个字符串。
使用字符串提供的 format 方法完成上述的拼接工作,如下所示:
>>> name = 'tom'>>> city = 'beijing'>>> text = 'My name is %s, I live in city %s.'>>> text.format(name, city)'My name is tom, I live in beijing.'
与上一节使用 + 运算连接相比,格式化字符串显著的提高了程序的可读性。
也可以使用运算符 % 完成上述的拼接工作,如下所示:
>>> name = 'tom'>>> city = 'beijing'>>> text = 'My name is %s, I live in city %s.'>>> text % (name, city)'My name is tom, I live in beijing.'
在实践中,通常直接将字符串与参数使用运算符 % 格式化,如下所示:
>>> name = 'tom'>>> city = 'beijing'>>> 'My name is %s, I live in city %s.' % (name, city)'My name is tom, I live in beijing.'
如果有多个参数,需要将所有的参数保存在一个元组中;如果只有一个参数,可以不用保存在元组中,举例如下:
>>> name = 'tom'>>> 'My name is %s.' % name'My name is tom.'
% 是一个特殊的符号,后面跟着一个字符用于说明参数的类型:
有多种类型的占位符,初学时需要掌握如下常用的占位符:
符号 | 描述 |
---|---|
%% | 用于表示 % |
%c | 格式化字符及其ASCII码 |
%s | 格式化字符串 |
%d | 格式化整数 |
%f | 格式化浮点数 |
>>> name = 'tom'>>> 'name is %s' % name'name is tom'
>>> integer = 123>>> 'integer is %d' % integer'integer is 123'
>>> float = 123.456>>> 'float is %f' % float'float is 123.456000'
>>> a = 3>>> b = 2>>> c = a % b>>> '%d %% %d = %d' % (a, b, c)3 % 2 = 1
%% 是一个特殊的占位符,表示字符 % 本身。
在实践中,通常使用 print 方法输出将格式化后的字符串,示例如下:
>>> name = 'tom'>>> city = 'beijing'>>> print('My name is %s' % name)
My name is tom>>> print('My name is %s, I live in city %s.' % (name, city))
My name is tom, I live in beijing.
字符串和数值一样,同样有基本的运算方式,但是最终得到的结果形式和数值却略有不同,大家在使用的时候要注意这一点。字符串的常用方法也需要大家多多练习和使用,可以把正文中的代码多敲几遍,一个好的程序员就是要多敲代码才能成长。