《轉》python學習(6)序列型別 字串

2021-09-07 10:06:45 字數 2638 閱讀 9822

**

二、序列型別

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

1、成員關係操作符(in / not in )

2、關於切片

1

2

3

4

5

6

s=[1,2,3,4]

prints[::-1]#下標範圍[0,0],步長是-1,則從後(4,包括4)往前切取所有,輸出:[4, 3, 2, 1]

prints[::-2]#下標範圍[0,0],步長是-2,則從後(4,包括4)往前跳過2位切取,輸出:[4, 2]

prints[::]#下標範圍[0,0],步長是0,則從前(1,包括1)往後切取所有,輸出:[1, 2, 3, 4]

prints[::2]#下標範圍[0,0],步長是2,則從前(1,包括1)往後跳過2位切取,輸出:[1, 3]

prints[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 >>> importstring

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學習11 序列

在 python 中,序列型別包括字串 列表 元組 集合和字典,這些序列支援一些通用的操作,但比較特殊的是,集合和字典不支援索引 切片 相加和相乘操作。例子 a list print a b i love lsgogroup b list b print b i l o v e l s g o g ...

192220序列型別

序列表示索引未非負整數的有序物件集合,包括字串,列表,元祖。說明 舉例 元祖跟裡面的逗號相關 mytuple a b c d type mytuple 列表根中括號相關 mylist a b c d type mylist 字串跟雙引號有關 mystr a,b,c,d type mystr 適用於所...

Python 學習筆記三 序列

sequence 序列 是一組有序的元素的集合,序列可以有任何元素,也可以沒有元素 元組與表的區別 一旦建立,tuple的各個元素不可再變更,而list的各個元素可以再變更 s1 1,2,zhansan 李四 false s2 1,2,zhansan lili true print s1,type ...