標籤:int 類型 檢查 產生 運行 參數 python學習 拼接 strip()
python的基礎資料型別 (Elementary Data Type)有數字、字串、列表、字典、元祖、布爾值
一、數字1.1、字元轉換為數字
執行個體:
a="123"b=int(a)print(b+100)
運行結果:
223
可以用type查看資料類型:
a="123"print(type(a))b=int(a)print(type(b))print(b+100)
運行結果:
<class ‘str‘><class ‘int‘>223
二、字串2.1、join()
將字串按照指定的字元進行拼接
執行個體:
#join(),將字串按照指定的字元進行拼接test="你女兒的媽媽的媽媽是誰"str_n="#"res=str_n.join(test)res1="&".join(test)print(res)print(res1)
運行結果:
你#女#兒#的#媽#媽#的#媽#媽#是#誰你&女&兒&的&媽&媽&的&媽&媽&是&誰
2.2、split()
split()通過指定分隔字元對字串進行切片,如果參數num 有指定值,則僅分隔 num 個子字串
文法:
str.split(str="", num=string.count(str))
參數:
str -- 分隔字元,預設為所有的Null 字元,包括空格、換行(\n)、定位字元(\t)等。
num -- 分割次數。
傳回值:字串列表
執行個體:
test="as d ef d qwe d ytrdvcd"v1=test.split(‘d‘,1)v2=test.split(‘d‘,2)v3=test.split(‘d‘,3)v4=test.split(‘d‘,4)print(v1)print(v2)print(v3)print(v4)
結果:
[‘as‘, ‘efdqwedytrdvcd‘][‘as‘, ‘ef‘, ‘qwedytrdvcd‘][‘as‘, ‘ef‘, ‘qwe‘, ‘ytrdvcd‘][‘as‘, ‘ef‘, ‘qwe‘, ‘ytr‘, ‘vcd‘]
2.3、find()方法
find() 方法檢測字串中是否包含子字串 str ,如果指定 beg(開始) 和 end(結束) 範圍,則檢查是否包含在指定範圍內,如果指定範圍內如果包含指定索引值,返回的是索引值在字串中的起始位置。如果不包含索引值,返回-1。
文法:
str.find(str, beg=0, end=len(string))
參數:
str:指定檢索的字串
beg:檢索的開始位置,預設為0
end:檢索的結束位置
傳回值:
如果包含子字串返回開始的索引值,否則返回-1。
執行個體:
test = "Weareallgoodfriends"test1 = "good"str=test.find(test1,0,len(test))str1 = test.find(test1)str2 = test.find(test1,2,19)str3 = test.find(test1,2,5)print(str)print(str1)print(str2)print(str3)
運行結果:
888-1
2.4、strip()方法
strip() 方法用於移除字串頭尾指定的字元(預設為空白格)。strip意思為清除、拆除、刪除的意思。
文法:
str.strip([chars])
參數:
chars -- 移除字串頭尾指定的字元。
傳回值:
返回移除字串頭尾指定的字元產生的新字串。
執行個體:
test="******Weareall***goodfriends*****"test1="*"str=test.strip(test1)print(str)print(test.strip(‘*‘))
運行結果:
Weareall***goodfriendsWeareall***goodfriends
2.5、lstrip()方法
strip() 方法用於移除字串左邊指定的字元(預設為空白格)。 left 為左邊的意思,strip意思為清除、拆除、刪除的意思。
文法:
str.strip([chars])
參數:
chars -- 移除字串左邊指定的字元。
傳回值:
返回移除字串左邊指定的字元產生的新字串。
執行個體:
test="******Weareall***goodfriends*****"test1="*"str=test.lstrip(test1)print(str)print(test.lstrip(‘*‘))
運行結果
Weareall***goodfriends*****Weareall***goodfriends*****
2.6、rsrip()方法
strip() 方法用於移除字串右邊指定的字元(預設為空白格)。 right為右邊的意思,strip意思為清除、拆除、刪除的意思。
文法:
str.strip([chars])
參數:
chars -- 移除字串右邊指定的字元。
傳回值:
返回移除字串右邊指定的字元產生的新字串。
執行個體:
test="******Weareall***goodfriends*****"test1="*"str=test.rstrip(test1)print(str)print(test.rstrip("*"))
運行結果
******Weareall***goodfriends******Weareall***goodfriends
2.7、upper()方法
upper() 方法將字串中的小寫字母轉為大寫字母。
文法:
str.upper()
參數:沒有
傳回值:
返回小寫字母轉為大寫字母的字串。
執行個體:
test="Weareallgoodfriends"print(test.upper())
運行結果:
WEAREALLGOODFRIENDS
2.8、lower()方法
lower() 方法將字串中的大寫字母轉為小寫字母。
文法:
str.lower()
參數:沒有
傳回值:
返回大寫字母轉為小寫字母的字串。
執行個體:
test="WeareallGOODfriends"print(test.upper())
運行結果:
WEAREALLGOODFRIENDS
2.9、replace()方法
replace() 方法把字串中的 old(舊字串) 替換成 new(新字串),如果指定第三個參數max,則替換不超過 max 次。
文法:
str.replace(old, new[, max])
參數:
old -- 將被替換的子字串。
new -- 新字串,用於替換old子字串。
max -- 可選字串, 替換不超過 max 次
傳回值:
返回字串中的 old(舊字串) 替換成 new(新字串)後產生的新字串,如果指定第三個參數max,則替換不超過 max 次。
執行個體:
test = "wertrecdezseytrer"v = test.replace("e",‘#‘)print(v)v = test.replace("e",‘#‘,2)print(v)
運行結果:
w#rtr#cd#zs#ytr#rw#rtr#cdezseytrer
python學習之基礎資料型別 (Elementary Data Type)