1、檔案首行用來指定編碼方式:
#-*- encoding:utf-8 -*-
2、變數:
(1)必須是由數字、字母、下劃線任意組合,且不能數字開頭,
(2)不能是python中的關鍵字['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif','else', 'except', 'exec', 'finally', 'for', 'from',
'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
(3)變數名必須具有可描述性,建議用下劃線隔開,例如:age_of_oldboy,
(4)不能是中文,
#設定變數x,y x = (1+2+3+4) print(x) print(x*5) y = x*5 print (y+100-45+2)
#變數指向 age1 = 12 age2 = age1 age3 = age2 age2 = 100 print (age1,age2,age3)
3、常量:
(1)π
(2)大寫一般為常量
(3)其他
BIR_OF_CHINA = 19491001
4、注釋:
單行注釋:#
多行注釋:'''注釋內容''' ,"""注釋內容"""
5、基礎資料型別初識:
print (x,type(x)) #判斷數字型別
(1)int 運算子:+, - ,*, / ,%, ** ,//,
(2)str python當中凡是用引號引起來的都是字串,
print("i'm a student") #字串可以用單引號/雙引號包括,如果字串內有單引號,就用雙引號包括,反之則用單引號, a = 'steven' b = 'lily' c = a + b #字串可相加:字串的拼接, print(c) $stevenlily d = a * 2 #字串符可想乘:字串列印幾次 str * int print (d)
msg = ''' 鋤禾日當午, 汗滴禾下土, 誰知盤中餐, 粒粒皆辛苦, ''' #三個單引號引起來并予以賦值變數,變成了一個長字串,而不是注釋的作用了, print(msg)
(3)bool 回傳 Ture or False
a = 3 b = 2 print (a>b) #判斷True False
6、用戶互動:input
(1)等待輸入
(2)將你輸入的內容賦值給了前面的變數
(3)input出來的資料型別全部是str
#用戶互動 name = input ('請輸入你的名字:') age = input ('請輸入你的年齡:') print ('我的名字是'+name,'我的年齡是'+age+'歲') $請輸入你的名字:x 請輸入你的年齡:y 我的名字是x 我的年齡是y歲
7、條件陳述句
(1)if:
if...if #兩個條件都會執行
if...elif #若if條件成立則不會執行elif條件,
if 5 > 4 : print (666) print (777) print (111) if True : print (666) print (777) if False : print (555) print(777)
if 4 > 5 : print ('我請你喝酒') else : print ('喝什么酒') if 4 > 5 : print ('我請你喝酒') print ('喝什么酒')
num = input ('請輸入您猜的數字:') if num == '1' : print ('恭喜你,答對了') elif num == '2' : print ('恭喜你,答對了') elif num == '3' : print ('恭喜你,答對了') else : print ('你猜錯了')
scroe = int(input('請輸入您的分數:')) #int(str)改變資料型別 if scroe >= 90 : print ('A') elif scroe >= 80 : print ('B') elif scroe >= 70 : print ('C') elif scroe >= 60 : print ('D') else : print ("you're fail!")輸入分數評定等級,90以上A,80以上B,70以上C,60以上D以及不及格
username = input ('請輸入用戶名:') password = input ('請輸入密碼:') if username == 'xinxin' : if password == 'abcd1234' : print ('歡迎您!!!') else : print ('密碼錯誤!!!') else : print ('無此賬號!!!')if的嵌套:輸入賬號密碼,正確提示歡迎,錯誤提示錯誤,賬號錯誤提示無此賬號
(2)while
a、無限回圈
#無限回圈 print('開始') while True : print('我們不一樣') print('在人間') print('癢') ptint('結束')
b、終止回圈:
改變條件,使其不成立,
#方法一: count = 1 flag = True #標志位 while flag : print (count) count = count + 1 if count > 100 : flag = False #方法二: count = 1 while count <= 100 : print (count) count = count + 1 #方法三 count = 1 while 1 : print (count) count = count + 1 if count > 100 : break計數1~100
# i = 0 # sum = 0 # while i < 100: # i = i + 1 # sum = sum + i # print(sum)相加1~100 break,直接終止
print('開始') while True: print('222') print('333') break print('444') print('結束')
i = 0 while 1: i = i + 1 if i > 100: break print(i)break計數1~100
continue,跳出回圈重新開始執行
print ('開始') count = 1 while count <= 100 : print (count) count = count + 1 continuecontinue計數1~100
i = 0 while i < 100: i = i +1 if 95 > i > 5: continue print(i)不回顯6到94間的數字
(3)while回圈使用else陳述句
count = 0 while count < 5: print(count,"小于5") count +=1 else: print(count,"大于或等于5")
(4)for 回圈:
ss = '1A2B3C4D5E6F!?' for i in ss: # i是變數,ss可以是串列,元組,字典,i 在 ss中有限回圈, print(i) #場景應用:敏感詞, sss = '你好,hello!' if 'hello' in sss: print('您的評論中有英文')
課后練習
#方法1: count = 0 while count < 101: print(count) count = count + 2 #方法2: count = 1 while count < 101: if count % 2 == 0: print (count) count = count + 1 #方法3: count = 1 while True : if count % 2 == 0 : print (count) count = count + 1 if count >=101 : break練習1:1-100求偶數
#方法1: count = 1 while count < 101: print(count) count = count + 2 #count += 2 #方法2: count = 1 while count < 101: if count % 2 == 1: print (count) count = count + 1 #方法3: count = 1 while True: if count % 2 == 1: print (count) count = count + 1 if count >=101: break練習2:1-100求奇數
#方法1: i = 1 sum1 = 0 sum2 = 0 while i < 100: if i % 2 == 0: sum1 = sum1 + i else: sum2 = sum2 + i i = i + 1 print(sum2 - sum1) #方法2: i = 1 sum = 0 while i < 100: if i % 2 == 0: sum = sum - i else: sum = sum + i i = i + 1 print(sum) #方法3: i = 0 j = -1 sum = 0 while i < 99: i = i + 1 j = -j if i % 2 == 0: sum = sum - i else: sum = sum + i print(sum)練習3:求1 - 2 + 3 - 4... + 99 所有數的和
# 方法1: i = 1 sum1 = 0 while i < 100: if i == 88: i = i + 1 continue elif i % 2 != 0: sum1 = sum1 + i else: sum1 = sum1 - i i = i + 1 print(sum1) #方法2: i = 0 j = -1 sum = 0 while i < 99: i = i + 1 j = -j if i == 88: continue else: sum = sum + i * j print(sum)拓展練習3:計算1 -2 + 3 ... + 99 中除了88以外的所有數的總和
# 計算1 -2 + 3 ... - 99 中除了88以外的所有數的總和 i = 1 sum1 = 0 while i < 100: if i == 88: i = i + 1 continue elif i % 2 != 0: if i == 99: i = i + 1 sum1 = sum1 - i sum1 = sum1 + i else: sum1 = sum1 - i i = i + 1 print(sum1)拓展練習3:計算1 -2 + 3 ... - 99 中除了88以外的所有數的總和
#方法1: count = 0 while count < 10: count = count + 1 if count == 7: print ('') else: print(count) #方法2:最優解 count = 0 while count < 10: count = count + 1 if count == 7: continue print(count) #方法3: count = 0 while count < 10: count = count + 1 if count == 7: pass else: print(count)練習4:使用while回圈輸入1 2 3 4 5 6 8 9 10
i = 0 while i < 3: username = input ('請輸入用戶名:') password = input ('請輸入密碼:') if username == 'xinxin' and password == 'abcd1234' : print ('歡迎您!!!') else : print ('錯誤!請重新輸入') i += 1練習5:用戶登錄(三次機會)
name = 'xinxin' pwd = '123456' i1 = 3 i2 = 3 while i2 > 0: mz = input('請輸入用戶名:') i1 = 3 if mz == name: while i1 > 0: mm = input('請輸入密碼:') i1 = i1 - 1 if mm == pwd: print('歡迎您,登錄成功') break else: print('請重新輸入密碼') if i1 == 0: print('次數已經用完,請重新登錄,') break else: print('用戶名錯誤,請重新輸入:') i2 = i2 - 1 if i2 == 0: print('次數已經用完,請重新登錄,') break已經實作用戶名密碼分別三次回圈
8、格式化輸出:
(1)%占位符
%s 字串
%d 數字
%% 百分號輸出
name = input('請輸入姓名:') age = input('請輸入年齡:') job = input('請輸入作業:') hobbie = input('請輸入興趣:') msg = '''--------------- info of %s --------------- name: %s age: %d job: %s hobbie:%s 我的心情指數:100%% --------------- end ---------------''' %(name,name,int(age),job,hobbie) print(msg)
username = 'xinxin' password = 'abcd1234' i = 0 while i < 3: name = input('請輸入賬號:') psw = input('請輸入密碼:') if name == username and psw == password: print('歡迎您') break else: print('賬號密碼錯誤,請重新輸入,剩下%d次機會' % (2 - i)) if (2-i) == 0: result =input('是否還想再試試?Yes') if result == 'Yes': i = 0 continue i = i + 1 else:print('goodbye') #思考自己寫的這個如何進入上述代碼的回圈 username = 'xinxin' password = 'abcd1234' i = 0 while i < 3: name = input('請輸入賬號:') psw = input('請輸入密碼:') if name == username and psw == password: print('歡迎您') break else: if name == username and psw != password: print('密碼錯誤,剩下%d次機會' % (2 - i)) elif name != username and psw != password: print('賬號密碼錯誤,請重新輸入,剩下%d次機會' % (2 - i)) i += 1用戶登錄(三次機會)
(2)format
# format 格式化輸出的三種玩法: # 第一種: s3 = '''my name is {}, i am {} year old, i like {}, again, i am {}.'''.format('xxx','02','playgame','xxx') print(s3) # 第二種: s4 = '''my name is {0}, i am {1} year old, i like {2}, again, i am {0}.'''.format('xxx','02','playgame') print(s4) n = input('name:') a = input('age:') h = input('hobby:') s4 = '''my name is {0}, i am {1} year old, i like {2}, again, i am {0}.'''.format(n,a,h) print(s4) # 第三種: n = input('name:') a = input('age:') h = input('hobby:') s5 = '''my name is {name}, i am {age} year old, i like {hobby}, again, i am {name}.'''.format(name=n,age=a,hobby=h) print(s5)
9、初始編碼:電腦的傳輸,儲存實際上都是用0和1來表示,
(1)ASCII碼:設計之初只為美國本土考慮,7位足以,作者預留了1位(bit),遂形成了8位表示一個位元組(byte),且所有的ASCII碼最左邊一位全部都是“0”
8bit == 1byte
1024byte == 1kb
1024kb == 1MB
1024MB == 1GB
1024GB == 1TB
| Bin(二進制) | Oct(八進制) | Dec(十進制) | Hex(十六進制) | 縮寫/字符 | 解釋 |
| 0000 0000 | 0 | 0 | 00 | NUL(null) | 空字符 |
| 0000 0001 | 1 | 1 | 01 | SOH(start of headline) | 標題開始 |
| 0000 0010 | 2 | 2 | 02 | STX (start of text) | 正文開始 |
| 0000 0011 | 3 | 3 | 03 | ETX (end of text) | 正文結束 |
| 0000 0100 | 4 | 4 | 04 | EOT (end of transmission) | 傳輸結束 |
| 0000 0101 | 5 | 5 | 05 | ENQ (enquiry) | 請求 |
| 0000 0110 | 6 | 6 | 06 | ACK (acknowledge) | 收到通知 |
| 0000 0111 | 7 | 7 | 07 | BEL (bell) | 響鈴 |
| 0000 1000 | 10 | 8 | 08 | BS (backspace) | 退格 |
| 0000 1001 | 11 | 9 | 09 | HT (horizontal tab) | 水平制表符 |
| 0000 1010 | 12 | 10 | 0A | LF (NL line feed, new line) | 換行鍵 |
| 0000 1011 | 13 | 11 | 0B | VT (vertical tab) | 垂直制表符 |
| 0000 1100 | 14 | 12 | 0C | FF (NP form feed, new page) | 換頁鍵 |
| 0000 1101 | 15 | 13 | 0D | CR (carriage return) | 回車鍵 |
| 0000 1110 | 16 | 14 | 0E | SO (shift out) | 不用切換 |
| 0000 1111 | 17 | 15 | 0F | SI (shift in) | 啟用切換 |
| 0001 0000 | 20 | 16 | 10 | DLE (data link escape) | 資料鏈路轉義 |
| 0001 0001 | 21 | 17 | 11 | DC1 (device control 1) | 設備控制1 |
| 0001 0010 | 22 | 18 | 12 | DC2 (device control 2) | 設備控制2 |
| 0001 0011 | 23 | 19 | 13 | DC3 (device control 3) | 設備控制3 |
| 0001 0100 | 24 | 20 | 14 | DC4 (device control 4) | 設備控制4 |
| 0001 0101 | 25 | 21 | 15 | NAK (negative acknowledge) | 拒絕接收 |
| 0001 0110 | 26 | 22 | 16 | SYN (synchronous idle) | 同步空閑 |
| 0001 0111 | 27 | 23 | 17 | ETB (end of trans. block) | 結束傳輸塊 |
| 0001 1000 | 30 | 24 | 18 | CAN (cancel) | 取消 |
| 0001 1001 | 31 | 25 | 19 | EM (end of medium) | 媒介結束 |
| 0001 1010 | 32 | 26 | 1A | SUB (substitute) | 代替 |
| 0001 1011 | 33 | 27 | 1B | ESC (escape) | 換碼(溢位) |
| 0001 1100 | 34 | 28 | 1C | FS (file separator) | 檔案分隔符 |
| 0001 1101 | 35 | 29 | 1D | GS (group separator) | 分組符 |
| 0001 1110 | 36 | 30 | 1E | RS (record separator) | 記錄分隔符 |
| 0001 1111 | 37 | 31 | 1F | US (unit separator) | 單元分隔符 |
| 0010 0000 | 40 | 32 | 20 | (space) | 空格 |
| 0010 0001 | 41 | 33 | 21 | ! | 嘆號 |
| 0010 0010 | 42 | 34 | 22 | " | 雙引號 |
| 0010 0011 | 43 | 35 | 23 | # | 井號 |
| 0010 0100 | 44 | 36 | 24 | $ | 美元符 |
| 0010 0101 | 45 | 37 | 25 | % | 百分號 |
| 0010 0110 | 46 | 38 | 26 | & | 和號 |
| 0010 0111 | 47 | 39 | 27 | ' | 閉單引號 |
| 0010 1000 | 50 | 40 | 28 | ( | 開括號 |
| 0010 1001 | 51 | 41 | 29 | ) | 閉括號 |
| 0010 1010 | 52 | 42 | 2A | * | 星號 |
| 0010 1011 | 53 | 43 | 2B | + | 加號 |
| 0010 1100 | 54 | 44 | 2C | , | 逗號 |
| 0010 1101 | 55 | 45 | 2D | - | 減號/破折號 |
| 0010 1110 | 56 | 46 | 2E | . | 句號 |
| 00101111 | 57 | 47 | 2F | / | 斜杠 |
| 00110000 | 60 | 48 | 30 | 0 | 數字0 |
| 00110001 | 61 | 49 | 31 | 1 | 數字1 |
| 00110010 | 62 | 50 | 32 | 2 | 數字2 |
| 00110011 | 63 | 51 | 33 | 3 | 數字3 |
| 00110100 | 64 | 52 | 34 | 4 | 數字4 |
| 00110101 | 65 | 53 | 35 | 5 | 數字5 |
| 00110110 | 66 | 54 | 36 | 6 | 數字6 |
| 00110111 | 67 | 55 | 37 | 7 | 數字7 |
| 00111000 | 70 | 56 | 38 | 8 | 數字8 |
| 00111001 | 71 | 57 | 39 | 9 | 數字9 |
| 00111010 | 72 | 58 | 3A | : | 冒號 |
| 00111011 | 73 | 59 | 3B | ; | 分號 |
| 00111100 | 74 | 60 | 3C | < | 小于 |
| 00111101 | 75 | 61 | 3D | = | 等號 |
| 00111110 | 76 | 62 | 3E | > | 大于 |
| 00111111 | 77 | 63 | 3F | ? | 問號 |
| 01000000 | 100 | 64 | 40 | @ | 電子郵件符號 |
| 01000001 | 101 | 65 | 41 | A | 大寫字母A |
| 01000010 | 102 | 66 | 42 | B | 大寫字母B |
| 01000011 | 103 | 67 | 43 | C | 大寫字母C |
| 01000100 | 104 | 68 | 44 | D | 大寫字母D |
| 01000101 | 105 | 69 | 45 | E | 大寫字母E |
| 01000110 | 106 | 70 | 46 | F | 大寫字母F |
| 01000111 | 107 | 71 | 47 | G | 大寫字母G |
| 01001000 | 110 | 72 | 48 | H | 大寫字母H |
| 01001001 | 111 | 73 | 49 | I | 大寫字母I |
| 01001010 | 112 | 74 | 4A | J | 大寫字母J |
| 01001011 | 113 | 75 | 4B | K | 大寫字母K |
| 01001100 | 114 | 76 | 4C | L | 大寫字母L |
| 01001101 | 115 | 77 | 4D | M | 大寫字母M |
| 01001110 | 116 | 78 | 4E | N | 大寫字母N |
| 01001111 | 117 | 79 | 4F | O | 大寫字母O |
| 01010000 | 120 | 80 | 50 | P | 大寫字母P |
| 01010001 | 121 | 81 | 51 | Q | 大寫字母Q |
| 01010010 | 122 | 82 | 52 | R | 大寫字母R |
| 01010011 | 123 | 83 | 53 | S | 大寫字母S |
| 01010100 | 124 | 84 | 54 | T | 大寫字母T |
| 01010101 | 125 | 85 | 55 | U | 大寫字母U |
| 01010110 | 126 | 86 | 56 | V | 大寫字母V |
| 01010111 | 127 | 87 | 57 | W | 大寫字母W |
| 01011000 | 130 | 88 | 58 | X | 大寫字母X |
| 01011001 | 131 | 89 | 59 | Y | 大寫字母Y |
| 01011010 | 132 | 90 | 5A | Z | 大寫字母Z |
| 01011011 | 133 | 91 | 5B | [ | 開方括號 |
| 01011100 | 134 | 92 | 5C | \ | 反斜杠 |
| 01011101 | 135 | 93 | 5D | ] | 閉方括號 |
| 01011110 | 136 | 94 | 5E | ^ | 脫字符 |
| 01011111 | 137 | 95 | 5F | _ | 下劃線 |
| 01100000 | 140 | 96 | 60 | ` | 開單引號 |
| 01100001 | 141 | 97 | 61 | a | 小寫字母a |
| 01100010 | 142 | 98 | 62 | b | 小寫字母b |
| 01100011 | 143 | 99 | 63 | c | 小寫字母c |
| 01100100 | 144 | 100 | 64 | d | 小寫字母d |
| 01100101 | 145 | 101 | 65 | e | 小寫字母e |
| 01100110 | 146 | 102 | 66 | f | 小寫字母f |
| 01100111 | 147 | 103 | 67 | g | 小寫字母g |
| 01101000 | 150 | 104 | 68 | h | 小寫字母h |
| 01101001 | 151 | 105 | 69 | i | 小寫字母i |
| 01101010 | 152 | 106 | 6A | j | 小寫字母j |
| 01101011 | 153 | 107 | 6B | k | 小寫字母k |
| 01101100 | 154 | 108 | 6C | l | 小寫字母l |
| 01101101 | 155 | 109 | 6D | m | 小寫字母m |
| 01101110 | 156 | 110 | 6E | n | 小寫字母n |
| 01101111 | 157 | 111 | 6F | o | 小寫字母o |
| 01110000 | 160 | 112 | 70 | p | 小寫字母p |
| 01110001 | 161 | 113 | 71 | q | 小寫字母q |
| 01110010 | 162 | 114 | 72 | r | 小寫字母r |
| 01110011 | 163 | 115 | 73 | s | 小寫字母s |
| 01110100 | 164 | 116 | 74 | t | 小寫字母t |
| 01110101 | 165 | 117 | 75 | u | 小寫字母u |
| 01110110 | 166 | 118 | 76 | v | 小寫字母v |
| 01110111 | 167 | 119 | 77 | w | 小寫字母w |
| 01111000 | 170 | 120 | 78 | x | 小寫字母x |
| 01111001 | 171 | 121 | 79 | y | 小寫字母y |
| 01111010 | 172 | 122 | 7A | z | 小寫字母z |
| 01111011 | 173 | 123 | 7B | { | 開花括號 |
| 01111100 | 174 | 124 | 7C | | | 垂線 |
| 01111101 | 175 | 125 | 7D | } | 倍訓括號 |
| 01111110 | 176 | 126 | 7E | ~ | 波浪號 |
| 01111111 | 177 | 127 | 7F | DEL (delete) |
(2) 萬國碼(Unicode)
ASCII碼為了解決全球化文字問題,創建了一個萬國碼Unicode,用4個位元組表示一個漢字,
后升級成為UTF-8,
gbk:中國國產,只能用于中文和ASCII碼的文字,
10、 運算子:以下假設變數:a=10,b=20
(1)算數運算

(2)比較運算:

(3)賦值運算:

(4)邏輯運算:

在沒有()的情況下not 優先級高于 and,and優先級高于or,即優先級關系為( )>not>and>or,同一優先級從左往右計算,
# int轉換成bool時,該值為非0時,為True,否則為False, print(bool(23)) $True print(bool(0)) $ False # bool轉換成int時,只有1和0 print(bool(true)) $1 print(bool(False)) $0
# x or y , x 為 True , 則回傳 x (x為非0) print(1 or 2) $ 1 print(3 or 2) $ 3 print(0 or 2) $ 2 print(0 or 100) $ 100 # x and y , x 為 True , 則回傳 x (and則與or的規則相反) print(1 and 2) $ 2 print(3 and 2) $ 2 print(0 and 2) $ 0 print(0 and 100) $ 0 # 面試題舉例 print(2 or 100 or 3 or 4) $ 2 print(0 or 4 and 3 or 2) $ 3 print(1 > 2 and 3 or 4 and 3 > 2) $True '''運算程序如下: False(0) and 3 or 4 and True(1) False or Ture True '''
(5)Python運算子優先級
以下表格列出了從最高到最低優先級的所有運算子:
| 運算子 | 描述 |
|---|---|
| ** | 指數 (最高優先級) |
| ~ + - | 按位翻轉, 一元加號和減號 (最后兩個的方法名為 +@ 和 -@) |
| * / % // | 乘,除,取模和取整除 |
| + - | 加法減法 |
| >> << | 右移,左移運算子 |
| & | 位 'AND' |
| ^ | | 位運算子 |
| <= < > >= | 比較運算子 |
| <> == != | 等于運算子 |
| = %= /= //= -= += *= **= | 賦值運算子 |
| is is not | 身份運算子 |
| in not in | 成員運算子 |
| not and or | 邏輯運算子 |
10、資料型別:
(1)int:用于計算,
(2)bool:True or False,用于判斷,
# 作業中常用的 while True: pass while 1: #1的效率要更高, pass # str 轉 bool,為空字串都是True,空就是什么都不輸入,不是空格, if s: #即表示 if s == " print('你輸入的為空,請重新輸入') else: pass
(3)str:存盤少量資料,進行操作,用引號 ' str ' 包括,
索引與切片:
# 索引,根據字串序號來進行索引,從左至右,從 0 開始,1 2 3 4 5 ... # 從右至左,從 -1 開始,-1 -2 -3 -4 -5 , s = 'ABCDEFGH' s1 = s[0] # 語法:變數名[序號] print(s1) # $ A s2 = s[-2] print(s2) # $ G #切片,顧頭不顧尾, s = 'ABCDEFGH' s3 = s[0:4] # 序號4是取不到的, print(s2) # $ ABC s4 = s[-3:-1] print(s4) # $ FG #加步長 變數名[首:尾:步長] s = 'ABCDEFGH' s5 = s[0:5:2] print(s5) # $ ACE s = 'ABCDEFGH' s6 = s[-1:-6:-2] print(s6) # $ HFD s7 = s[5:2:-1] print(s7) # $ FED s8 = s[-1:-4:-1] print(s8) # $ HGF s9 = s[0:] print(s8) # $ HGF #取全部值, s = 'ABCDEFGH' s9 = s[0:] print(s9) # $ ABCDEFGH s10 = s[::-1] print(s10) # $ HGFEDCBA
字串其他操作
s = 'xCm xl xXx' print(s.capitalize()) # 首字母大寫 print(s.upper()) # 全大寫 print(s.lower()) # 全小寫 print(s.swapcase()) # 大小寫翻轉 print(s.title()) # 每個隔開(特殊字符或數字)的單詞首字母大寫 print(s.count('x')) # 統計該元素個數有幾個, print(s.split(' ')) # 字串以指定引數內的元素左右分隔成新的串列, s1 = 'xXinXin大姐姐' print(s1.center(20,'~')) # 居中,空白填充 print(s1.startswith('xx')) # 判斷字串以什么開頭 print(s1.startswith('i',2,5)) # 切片查找i開頭的 print(s1.find('X')) # 通過元素找索引,找不到回傳 -1 print(s1.index('X')) # 通過元素找索引,找不到會報錯, print(s1.replace('Xin','liang',2)) print(len(s1)) # 公共方法
# 應用場景:轉換/t,多用于各種財務報表 str = "runoob\t12345\tabc" # 將/t轉換成4個空格,括號內引數4,表示幾個空格, print(str.expandtabs(4))
# 應用場景:驗證碼,加入數字無影響 s_str = 'xCDvi' you_input = input('請輸入驗證碼,不區分大小寫') if s_str.upper() == you_input.upper(): print('輸入成功') else: print('請重新輸入')
# 應用場景:默認洗掉字串前后空格, username = input('請輸入名字:').strip() if username == 'xxx': print('you are beautiful') s2 = 'lsplsplsplsp' # 引數內定義元素也可洗掉前后元素 print(s2.strip('lp')) # 也可以指定方向,rstrip 和 lstrip .
# is系列 name = 'xxinxin688' print(name.isalnum()) # 字串由字母或數字組成 print(name.isalpha()) # 字串只由字母組成 print(name.isdecimal()) # 字串只由數字組成
(4)list:存盤大量資料,用中括號 [ list ] 包括,
(5)tuple:元組又叫只讀串列,用小括號 ( tuple ) 包括,
(6)dict:儲存關系型資料,字典有對應的鍵值,用于大括號 { dict } 包括,
(7)集合:也是用于大括號 { 集合 } 包括,里面也是用儲存資料,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/226122.html
標籤:Python
