目录
1 概述
在前面我们已经分享了Python思维导图,这一节,我们讲解Python基本数据类型:元组与字符串。
1.1 元组
1.2 字符串
2 元组
在 Python 中,元组是一种不可变序列,它使用圆括号来表示:1. >>> a = ( 1 , 2 , 3 ) # a 是一个元组2. >>> a3. ( 1 , 2 , 3 )4. >>> a [ 0 ] = 6 # 元组是不可变的,不能对它进行赋值操作5. Traceback ( most recent call last ):6. File "<stdin>" , line 1 , in < module >7. TypeError : 'tuple' object does not support item assignment
2.1 空元组
创建一个空元组可以用没有包含内容的圆括号来表示:
1. >>> a = ()2. >>> a3. ()
2.2 一个值的元组
创建一个值的元组需要在值后面再加一个逗号, 这个比较特殊,需要牢牢记住 :1. >>> a = ( 12 ,) # 在值后面再加一个逗号2. >>> a3. ( 12 ,)4. >>> type ( a )5. < type 'tuple' >6. >>>7. >>> b = ( 12 ) # 只是使用括号括起来,而没有加逗号,不是元组,本质上是 b = 128. >>> b9. 1210. >>> type ( b )11. < type 'int' >
2.3 元组操作
元组也是一种序列,因此也可以对它进行索引、分片等。由于它是不可变的,因此就没有类似列表的append, extend, sort 等方法。
3 字符串
3.1 字符串简介
字符串也是一种序列,因此,通用的序列操作,比如索引,分片,加法,乘法等对它同样适用。比如:
1. >>> s = 'hello, '2. >>> s [ 0 ] # 索引3. 'h'4. >>> s [ 1 : 3 ] # 分片5. 'el'6. >>> s + 'world' # 加法7. 'hello, world'8. >>> s * 2 # 乘法9. 'hello, hello, '但需要注意的是,字符串和元组一样,也是不可变的,所以你不能对它进行赋值等操作:1. >>> s = 'hello'2. >>> s [ 1 ] = 'ab' # 不能对它进行赋值3. Traceback ( most recent call last ):4. File "<stdin>" , line 1 , in < module >5. TypeError : 'str' object does not support item assignment
除了通用的序列操作,字符串还有自己的方法,比如 join, lower, upper 等。字符串的方法特别多,这里只介绍一些常用的方法,如下:
3.2 find
find 方法用于在一个字符串中查找子串,它返回子串所在位置的最左端索引,如果没有找到,则返回-1。看看例子:1. >>> motto = "to be or not to be, that is a question"2. >>> motto . find ( 'be' ) # 返回 'b' 所在的位置,即 33. 34. >>> motto . find ( 'be' , 4 ) # 指定从起始位置开始找,找到的是第 2 个 'be'5. 166. >>> motto . find ( 'be' , 4 , 7 ) # 指定起始位置和终点位置,没有找到,返回 -17. - 1
3.3 split
split 方法用于将字符串分割成序列。看看例子:1. >>> '/user/bin/ssh' . split ( '/' ) # 使用 '/' 作为分隔符2. [ '' , 'user' , 'bin' , 'ssh' ]3. >>> '1+2+3+4+5' . split ( '+' ) # 使用 '+' 作为分隔符4. [ '1' , '2' , '3' , '4' , '5' ]5. >>> 'that is a question' . split () # 没有提供分割符,默认使用所有空格作为分隔符6. [ 'that' , 'is' , 'a' , 'question' ]需要注意的是,如果不提供分隔符,则默认会使用所有空格作为分隔符(空格、制表符、换行等)。
3.4 join
join 方法可以说是 split 的逆方法,它用于将序列中的元素连接起来。看看例子:1. >>> '/' . join ([ '' , 'user' , 'bin' , 'ssh' ])2. '/user/bin/ssh'3. >>>4. >>> '+' . join ([ '1' , '2' , '3' , '4' , '5' ])5. '1+2+3+4+5'6. >>> ' ' . join ([ 'that' , 'is' , 'a' , 'question' ])7. 'that is a question'8. >>> '' . join ([ 'h' , 'e' , 'll' , 'o' ])9. 'hello'10. >>> '+' . join ([ 1 , 2 , 3 , 4 , 5 ]) # 不能是数字11. Traceback ( most recent call last ):12. File "<stdin>" , line 1 , in < module >13. TypeError : sequence item 0 : expected string , int found
3.5 strip
strip 方法用于移除字符串左右两侧的空格,但不包括内部,当然也可以指定需要移除的字符串。看看例子:1. >>> ' hello world! ' . strip () # 移除左右两侧空格2. 'hello world!'3. >>> '%%% hello world!!! ####' . strip ( '%#' ) # 移除左右两侧的 '%' 或 '#'4. ' hello world!!! '5. >>> '%%% hello world!!! ####' . strip ( '%# ' ) # 移除左右两侧的 '%' 或 '#' 或空格6. 'hello world!!!'
3.6 replace
replace 方法用于替换字符串中的所有匹配项。看看例子:1. >>> motto = 'To be or not To be, that is a question'2. >>> motto . replace ( 'To' , 'to' ) # 用 'to' 替换所有的 'To' ,返回了一个新的字符串3. 'to be or not to be, that is a question'4. >>> motto # 原字符串保持不变5. 'To be or not To be, that is a question'
3.7 translate
translate 方法和 replace 方法类似,也可以用于替换字符串中的某些部分,但 translate 方法只处理单个字符。translate 方法的使用形式如下:str.translate(table[, deletechars])
其中, table 是一个包含 256 个字符的转换表,可通过 maketrans 方法转换而来,deletechars 是字符串中要过滤的字符集。看看例子:1. >>> from string import maketrans2. >>> table = maketrans ( 'aeiou' , '12345' )3. >>> motto = 'to be or not to be, that is a question'4. >>> motto . translate ( table )5. 't4 b2 4r n4t t4 b2, th1t 3s 1 q52st34n'6. >>> motto7. 'to be or not to be, that is a question'8. >>> motto . translate ( table , 'rqu' ) # 移除所有的 'r', 'q', 'u'9. 't4 b2 4 n4t t4 b2, th1t 3s 1 2st34n'可以看到, maketrans 接收两个参数:两个等长的字符串,表示第一个字符串的每个字符用第二个字符串对应位置的字符替代,在上面的例子中,就是 ‘a’ 用 ‘1’ 替代, ’e’ 用 ‘2’ 替代,等等,注意,是单个字符的代替,而不是整个字符串的替代。因此,motto 中的 o 都被替换为 4 , e 都被替换为 2 ,等等。
3.8 lower/upper
lower/upper 用于返回字符串的大写或小写形式。看看例子1. >>> x = 'PYTHON'2. >>> x . lower ()3. 'python'4. >>> x5. 'PYTHON'6. >>>7. >>> y = 'python'8. >>> y . upper ()9. 'PYTHON'10. >>> y11. 'python'