1. 程式人生 > >Python全棧之路---資料型別—字串

Python全棧之路---資料型別—字串

字串:有序的字元的集合,用於儲存和表示基本的文字資訊,一對單、雙、或三引號中間包含的內容稱之為字串

     1.特性:有序,不可變(開闢新地址儲存字串,python直譯器會定期清空不用了的已儲存的)

>>> a = 'alex'
>>> id(a)
1806635111064
>>> a = 'Jack'
>>> a
'Jack'
>>> id(a)
1806635111232

     2.操作  常用:isdigit  replace  find  count  strip  center  split  format  join

(1)得出資料的操作:

 1 >>> s = 'Hello World!'
 2 >>> s.swapcase() #大小寫反過來
 3 'hELLO wORLD!'
 4 >>> s = 'Hello World!'
 5 >>> s.capitalize() #第一個字母大寫,其餘全部小寫
 6 'Hello world!'
 7 >>> s.casefold() #變成統一的小寫字母
 8 'hello world!'
 9 >>> s.center(50,'-') #把s放到中間,兩邊用'-'填充,總長度為50
10 '-------------------Hello World!-------------------' 11 >>> s.count('o') #統計字元'o'的數量 12 2 13 >>> s.count('o',0,5) #統計從0到5字元'o'的數量,顧頭不顧尾 14 1 15 >>> s2 = 'a\tb' #\b是Tab鍵 16 >>> s2 17 'a\tb' 18 >>> print(s2) 19 a b 20 >>> s2.expandtabs() 21 'a b' 22
>>> s2.expandtabs(20) #將Tab鍵的長度擴充套件到20 23 'a b' 24 >>> s.find('o') #查詢字元o 找到返回索引值,找不到返回負數 25 4 26 >>> s.find('op') 27 -1 28 >>> s.find('o',0,3) #從0到3查詢字元o,找到返回從左邊找到的第一個索引值,找不到返回-1,rfind -> 從右邊開始數 29 -1 30 >>> s.find('o',0,5) #顧頭不顧尾 31 4 32 >>> s3 = 'my name is {0}, i am {1} years old.' 33 >>> s3 34 'my name is {0}, i am {1} years old.' 35 >>> s3.format('Alex','22') #格式化,用括號裡的依次代替{0}{1},有點像列表 36 'my name is Alex, i am 22 years old.' 37 >>> s3 = 'my name is {0}, his name is {0}.' 38 >>> s3.format('Alex') #括號裡的第一個就是{0},兩個{0}一樣 39 'my name is Alex, his name is Alex.' 40 >>> s3.format('Alex','Jane') 41 'my name is Alex, his name is Alex.' 42 >>> s3 = 'my name is {name}, i am {age} years old.' 43 >>> s3.format(name = 'Alex',age = '22') #根據定義命名來賦值,直接寫是不行的 44 'my name is Alex, i am 22 years old.' 45 >>> s.index('o') #返回第一個o的索引值,rindex -> 從右邊開始數,其餘一致 46 4 47 >>> s.index('o',5,6) #從5到6返回從左邊數第一個o的索引值,沒有就報錯 48 Traceback (most recent call last): 49 File "<stdin>", line 1, in <module> 50 ValueError: substring not found 51 >>> names = ['alex','jack','rain'] 52 >>> '-'.join(names) #以 - 來將names裡的元素拼接起來,並且進行區分 53 'alex-jack-rain' 54 >>> ''.join(names) 55 'alexjackrain' 56 >>> ','.join(names) 57 'alex,jack,rain' 58 >>> s = 'Hello World' 59 >>> s.ljust(50) #把字串的長度變成50,長度不夠,就在字串竄後面用空格來填 60 'Hello World ' 61 >>> s.ljust(50,'-') #把字串的長度變成50,長度不夠,就在字串後面用 - 來填,rjust與ljust相反,rjust在左邊填 - 62 'Hello World---------------------------------------' 63 >>> s.zfill(40) #把s長度變成40,不夠的在前面用0y填,一般是底層二進位制用到 64 '00000000000000000000000000000hello world' 65 >>> s = '\n hello world ' 66 >>> s 67 '\n hello world ' 68 >>> s.strip() #去掉前後的空格 lstrip ->只去掉左邊的空格 rstrip ->只去掉右邊的空格 69 'hello world' 70 >>> str_in = 'abcdef' 71 >>> str_out = '[email protected]#$%^' # str_in和str_out的字元數量必須一致,如此才能一一對應 72 >>> str.maketrans(str_in,str_out) #maketrans生成密碼對應表,表中為ASCII碼 73 {97: 33, 98: 64, 99: 35, 100: 36, 101: 37, 102: 94} 74 >>> table = str.maketrans(str_in,str_out) 75 >>> table 76 {97: 33, 98: 64, 99: 35, 100: 36, 101: 37, 102: 94} 77 >>> s = 'hello world' 78 >>> s.translate(table) #將s按照table進行翻譯 79 'h%llo worl$' 80 >>> s.partition('o') #將字串以從左找到的第一個'o'分成兩半,rpartition以從右邊找到的第一個'o'分成兩半 81 ('hell', 'o', ' world') 82 >>> s.replace('o','-') #把'o'全部換成'-' 83 'hell- w-rld' 84 >>> s.replace('o','-',1) #把'o'換成'-',只換一次 85 'hell- world' 86 >>> s.split() #以空格將s分開,放入列表 87 ['hello', 'world'] 88 >>> s.split('o') #以'o'將s分開,放入列表,'o'消失 89 ['hell', ' w', 'rld'] 90 >>> s.split('o',1) #以左邊第一個'o'將s分開,放入列表,'o'消失,rsplit -> 從右邊開始數 , splitlines -> 以行,即'\n'來分 91 ['hell', ' world']

(2)判斷 True or False 的操作

 1 >>> s.endswith('!') #s是否以!結尾,startswith -> 以...開始
 2 True
 3 >>> s.endswith('!sdf') #s是否以!sdf結尾
 4 False
 5 >>> '22'.isalnum() #判斷是否是數字和字元,不包括特殊符號
 6 True
 7 >>> '22s'.isalnum()
 8 True
 9 >>> '22s!'.isalnum()
10 False
11 >>> '33'.isdecimal() #判斷是否是整數,用法和  isdigit  、  isnumeric
12 True
13 >>> '33s'.isdecimal()
14 False
15 >>> '33.3'.isdecimal()
16 False
17 >>> '33'.isidentifier() #是否是合法的變數名,合法變數名條件:以下劃線、字母、數字組成,開頭不能是數字,不能純用數字組合
18 False
19 >>> '3asd'.islower() #字母是否都是小寫字母
20 True
21 >>> '2asd56ER'.islower()
22 False
23 >>> '33'.isprintable() #能否被列印,只有文字檔案,位元組格式的能被列印,純二進位制檔案之類的不能被列印(Linux中涉及)
24 True
25 >>> ' '.isspace() #是否是空格
26 True
27 >>> '3de '.isspace()
28 False
29 >>> 'Hello World'.istitle() #單詞的第一個字元是否都是大寫,就像新聞標題一樣
30 True
31 >>> '45ASD'.isupper() #字母是否都是大寫
32 True
33 >>> '45sA'.isupper()
34 False