《转》python学习(6)序列类型-字符串

时间:2023-03-09 00:05:25
《转》python学习(6)序列类型-字符串

转自 http://www.cnblogs.com/BeginMan/archive/2013/06/08/3125502.html

二、序列类型

包含字符串、列表、元祖。模式都一样,举一反三即可。如:

1、成员关系操作符(in / not in )

2、关于切片

1
2
3
4
5
6
s=[1,2,3,4]
print s[::-1]     #下标范围[0,0],步长是-1,则从后(4,包括4)往前切取所有,输出:[4, 3, 2, 1]
print s[::-2]     #下标范围[0,0],步长是-2,则从后(4,包括4)往前跳过2位切取,输出:[4, 2]
print s[::]        #下标范围[0,0],步长是0,则从前(1,包括1)往后切取所有,输出:[1, 2, 3, 4]
print s[::2]       #下标范围[0,0],步长是2,则从前(1,包括1)往后跳过2位切取,输出:[1, 3]
print s[1:4:2#下标范围[1,4],步长是2,则从下标为1(2)到下标为4(4)跳过2位切取,输出:[2, 4]

要灵活运用。

三、关于序列类型的内建函数

如list()、tuple()、str()类型转换,实际上是工厂函数,浅copy的结果而并非真正的改头换面(转换)。

注意在string类型上应用list()、tuple()往往并不能得到我们想要的结果。

序列类型的内建函数一览表:

cmp()、len()、max()、min()、enumerate()、zip()、

四、Unicode字符串

1 >>> 'hello'+u' '+'world'
2 u'hello world'

五、字符串类型的内建方法

1、不常用的string模块

 1 >>> import string
2 >>> string.uppercase
3 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
4 >>> string.lowercase
5 'abcdefghijklmnopqrstuvwxyz'
6 >>> string.whitespace
7 '\t\n\x0b\x0c\r '
8 >>> string.digits
9 '0123456789'
10 >>> string.punctuation
11 '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

打开这个string.py模块,如下:

.......................................
# Some strings for ctype-style character classification
whitespace = ' \t\n\r\v\f'
lowercase = 'abcdefghijklmnopqrstuvwxyz'
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
letters = lowercase + uppercase
ascii_lowercase = lowercase
ascii_uppercase = uppercase
ascii_letters = ascii_lowercase + ascii_uppercase
digits = '0123456789'
hexdigits = digits + 'abcdef' + 'ABCDEF'
octdigits = '01234567'
punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
printable = digits + letters + punctuation + whitespace # Case conversion helpers
.......................................

不常用,很多功能可以自己模拟。

2、内建函数
《转》python学习(6)序列类型-字符串

《转》python学习(6)序列类型-字符串

《转》python学习(6)序列类型-字符串

join/split:http://www.cnblogs.com/BeginMan/archive/2013/03/21/2972857.html