基本資料類之內建方法
阿新 • • 發佈:2022-03-09
基本資料類之內建方法
一.字串的內建方法(二)
1.移除字串首尾的指定字元,可以選擇方向
s = '###oscar###' print(s.strip('#')) # oscar 預設首位都去除 print(s.lstrip('#')) # oscar### 去除左邊 print(s.rstrip('#')) # ###oscar 去除右邊
2.大小相關操作
# 大小寫相關操作 s1 = 'OsCar Is BoY' print(s1.lower()) # 把字串的字母全部換成小寫 oscar is boy print(s1.upper()) # 把字串的字母全部換成大寫 OSCAR IS BOYprint(s1.islower()) # 判斷字串的字母是否全部是小寫 結果返回布林值 print(s1.isupper()) # 判斷字串的字母是否全部是大寫 結果返回布林值
利用這個小知識點我們可以做一個經常見的驗證碼功能:
# 圖片驗證碼 l = 'Oas125A' print('呈現給使用者的%s:'%l) i = input('輸入驗證碼:').strip() if l.lower() == i.lower(): # 在這裡同意換成小寫 print('驗證碼正確') else: print('驗證碼錯誤')
3.判斷字串的首尾是否是指定的字元
# 判斷字串的首尾是否是指定的字元s3 = 'oscar tony jason' print(s3.startswith('o')) # 判斷開頭的字母,返回結果是布林值 True print(s3.startswith('oscar')) # 也可以是多個字元 True print(s3.startswith('tony')) # False print(s3.endswith('n')) # 判斷結尾的字母 True print(s3.endswith('jason')) # 也可以是多個字元
4.格式化輸出
(1).佔位符(就是根據%s、%d來佔位)
(2).format方法:
方式一:跟佔位符差不多
('my name is {} ,age is {}'.format('oscar',21)) # 用花括號{}來佔位
方式二:根據索引取值,可以反覆使用
print('my name is {0} {0} ,age is {1} {0}'.format('oscar',21)) # 根據下標來索引取值
方式三:直接指定
print('my name is {name},age is {age}'.format(name = 'oscar', age = 21)) # 直接指名道姓
方式四:直接使用已經出現過的變數
name = 'oscar' age = 21 print('my name is {name},age is {age}') # 直接輸入變數名
5.拼接字串
# 拼接字串 s4 = '你好' s5 = '我是oscar' print(s4 + s5) # 連線字串 你好我是oscar print(s4 * 5) # 重複某個字串,重複幾遍就乘多少 你好你好你好你好你好 # join方法相當於將括號內的元素進行for迴圈 print(','.join(s5)) # 我,是,o,s,c,a,r print('|'.join(['oscar','tony','jason'])) # oscar|tony|jason # 注意:join的元素必須是字串,否則報錯
6.替換字串中指定的字元
# 替換字串中指定的字元 s6 = 'oscar oscar oscar oscar oscar' print(s6.replace('oscar','jason')) # jason jason jason jason jason print(s6.replace('oscar','jason',2)) # 也可以指定替換的個數,從左往右數 jason jason oscar oscar oscar
7.判斷字串是否是純數字
# 判斷字串是否是純數字 print('oscar123'.isdigit()) # 判斷字串是否是純數字組成,返回結果是布林值 False print('12.23'.isdigit()) # 帶小數點也不行 False print('123456'.isdigit()) # 必須是純數字 True
有這個知識點我們就可以解決成績判斷不小心輸入字母或小數點的問題:
score = input('>>:') if score.isdigit(): # 判斷是否是純數字 score = int(score) else: print('輸錯了')
瞭解操作
1.查詢指定字元對應的位置
# 查詢指定字元對應的位置 s8 = 'oscar is boy' print(s8.find('r')) # 從左往右查詢,找到一個就結束 print(s8.find('b', 0, 7)) # 在一個指定的範圍尋找,-1就是沒有找到 print(s8.index('s')) # 從左往右查詢,找到一個就結束 print(s8.index('b', 0 , 7)) # index找不到就會報錯,不建議使用
2.文字位置改變
# 文字位置改變 s9 = 'oscar' print(s9.center(15, '*')) # 字串在中間,其餘的用“*”補充 *****oscar***** print(s9.ljust(15, '*')) # 字串在左邊,其餘的用“*”補充 oscar********** print(s9.rjust(15, '*')) # 字串在右邊,其餘的用“*”補充 **********oscar print(s9.zfill(15)) # 字串在右邊,其餘的用“0”補充 0000000000oscar
3.特殊符號:斜槓與一些一些英文字母的組合會產生特殊的含義
# 特殊符號 print('oscar\njason\tto\any') # \n換行符,\t四個空格,\a:“” print(r'oscar\njason\tto\any') # 前面加一個“r”就可以取消特殊含義 oscar\njason\tto\any
4.captalize,swapcase,title
(1).首字母大寫
# 首字母大寫 s = 'oscar is boy' print(s.capitalize()) # Oscar is boy
(2).大小寫翻轉
# 大小寫翻轉 s = 'oscar IS Boy' # OSCAR is bOY print(s.swapcase())
(3).每個單詞首字母大寫
# 每個單詞的首字母大寫 s = 'oscar IS Boy' # Oscar Is Boy print(s.title())
列表內建方法
1.型別轉換
列表型別轉換支援遵循for迴圈的資料型別,所以可以進行列表資料型別轉換的基本資料型別有:字典、字串、元組、集合。
# 型別轉換 print(list(11)) # 報錯 print(list(11.22)) # 報錯 print(list(True)) # 報錯 print(list('oscar')) # ['o', 's', 'c', 'a', 'r'] print(list({'name':'oscar','age': 21})) # ['name', 'age'] print(list({11, 22, 33})) # [33, 11, 22] print(list((11, 22, 33))) # [11, 22, 33]
2.索引取值
# 索引取值 s = ['oscar', 'jason', 11, 22] print(s[2])
3.切片操作
# 切片操作 s = ['oscar', 'jason', 11, 22] print(s[0:3]) # ['oscar', 'jason', 11] print(s[-2:-1]) # [11]
4.步長
# 步長 s = ['oscar', 'jason', 11, 22] print(s[0:3]) # ['oscar', 'jason', 11] print(s[0:3:2]) # ['oscar', 11]
5.統計列表中元素的個數
# 統計列表中元素的個數 s = ['oscar', 'jason', 11, 22] print(len(s)) # 4
6.成員運算,最小判斷單位是元素,不是元素中的字元
# 成員運算 s = ['oscar', 'jason', 11, 22] print(11 in s) # True print(1 in s) # False
7.列表新增元素
方式一:在尾部新增單個元素
# 在尾部新增元素 s = ['oscar', 'jason', 11, 22] s.append('tony') print(s) # ['oscar', 'jason', 11, 22, 'tony']
方式二:在指定位置新增單個元素
# 在指定位置新增元素 s = ['oscar', 'jason', 11, 22] s.insert(2, 'tony') print(s) # ['oscar', 'jason', 'tony', 11, 22]
方式三:合併列表
# 合併列表 s = ['oscar', 'jason', 11, 22] s1 = ['tony', 33, 55] s.extend(s1) print(s) # ['oscar', 'jason', 11, 22, 'tony', 33, 55]
8.刪除元素
方式一:通用的刪除方式
# 通用刪除方式 s = ['oscar', 'jason', 11, 22] del s[1] print(s) # ['oscar', 11, 22]
方式二:指定的直接刪除元素
# 指定直接刪除元素 s = ['oscar', 'jason', 11, 22] print(s.remove('oscar')) # 直接刪除,不在使用 None print(s) # s = ['oscar', 'jason', 11, 22]
方式三:延遲刪除
# 延遲刪除 s = ['oscar', 'jason', 11, 22] print(s.pop()) # 22 預設是最後一位,還可以呼叫 print(s) # ['oscar', 'jason', 11] print(s.pop(1)) # jason 可以指定 print(s) # ['oscar', 11]
9.修改列表元素
# 修改列表元素 s = ['oscar', 'jason', 11, 22] s[1] = 'tony' print(s) # 指定元素修改 ['oscar', 'tony', 11, 22]
10.排序
# 排序 s = [11, 22, 99, 22, 44, 66] s.sort() # 預設是升序 print(s) # [11, 22, 22, 44, 66, 99] s.sort(reverse=True) # 修改為降序 print(s) # [99, 66, 44, 22, 22, 11]
11.翻轉
# 翻轉 s = [11, 22, 99, 22, 44, 66] s.reverse() # 前後顛倒 print(s) # [66, 44, 22, 99, 22, 11]
12.比較運算
# 比較運算 s = [11, 22, 99, 22, 44, 66] s1 = [33, 1, 2, 3] print(s1 > s) # True 列表比較的時候,比較的是相同索引位置上的元素,有一個出來結果,後面的就不在做比較
13.統計列表中某個元素出現的次數
# 統計列表中某個元素出現的次數 s = [11, 22, 22, 33, 11, 55, 11, 33, 66, 22, 44] print(s.count(11)) # 3
14.清除列表中元素
# 清除元素 s = [11, 22, 22, 33, 11, 55, 11, 33, 66, 22, 44] s.clear() print(s) # 清除所有元素 []
可變型別和不可變型別
可變型別:值改變,記憶體地址不變,改變的是本身
不可變型別:值改變,記憶體地址肯定改變,修改過程中產生了新的值
檢視記憶體地址的方法:id關鍵字
# 不可變型別 字串 s = '##oscar##' print(id(s.strip('#'))) # 2604580852600 print(id(s)) # 2604580875440
# 可變型別 列表 s = [11, 22, 33] print(id(s)) # 3094027050248 s.append(111) print(id(s)) # 3094027050248
佇列與堆疊
佇列:先進先出
# 佇列
s = []
# 先進
s.append(1)
s.append(2)
s.append(3)
print(s)
# 先出
for i in s:
print(i)
print(s.pop(0))
print(s.pop(0))
print(s.pop(0))
堆疊:先進後出
# 堆疊 s = [] # 先進 s.append(1) s.append(2) s.append(3) print(s) # 後出 print(s.pop()) print(s.pop()) print(s.pop())