python小筆記 字串3

2021-08-21 22:18:30 字數 4208 閱讀 3229

切割

split(str=」」[[,num])

(以str為分隔符擷取字串,指定num,則僅僅擷取num個字串)

eg:str = 「sunck**is****a*good**man」

print(str.split(「*」)) 返回['sunck', '', 'is', '', '', '', 'a', 'good', '', 'man']

print(str.split(「*」,3)) 返回['sunck', '', 'is', '***a*good**man']

splitline([keepends]})

(按照』\r』,』\n』,』\r\n』分隔,返回。keepends=ture時會保留換行符,預設為false)

eg:str = 『』』sunckisa

man『』』

print(str.splitline()) 返回['sunck', '       is', '       a', '       man']

print(str.splitline()) 返回['sunck\n', '       is\n', '       a\n', '       man']

組合

join(seq)

(以指定的字串分隔符,將seq中的所有元素組合成乙個字串)

eg:list = [『sunck』,』is』,』good』]

str = 「**」,join(list)

print(str) 返回sunck**is**good

max()  min()

eg:str = 「sunck is a good man」

print(max(str)) 返回u

print(「*」+min(str)+」*」) 返回* *

replace(oldstr,newstr,count)

(用newstr替換oldstr,預設是全部替換。如果制動了count,那麼只替換前count個)

eg:str = 「sunck is a good man good」

print(str.replace(「good」,」nice」) 返回sunck is a nice man nice

print(str.replace(「good」,」nice」,1) 返回sunck is a nice man good

maketrans()

(建立乙個字串對映表

)eg:t = str.maketrans(「good」,」2345」)

t1 = str.maketrans(「abc」,」abc」)

str = 「sunck is a good man good」

print(str.translate(t)) 返回sunck is a 2345 man2345

print(str.translate(t1)) 返回sunck is a good man good

startwith(str[,start=0][end=len(str)])

(在給定的範圍內判斷是否已給定的字串開頭,如果沒有指定範圍,預設整個字串長度)

eg: str = 「sunck is a good man good」

print(str.startwith(「sunck」)) 返回true

print(str.startwith(「sunck」,5,16) 返回false

endwith(str[,start=0][end=len(str)])

(在給定的範圍內判斷是否已給定的字串結尾,如果沒有指定範圍,預設整個字串長度)

eg: str = 「sunck is a good man good」

print(str.startend(「good」)) 返回true

encode(encoding=utf-8,errors=strict編碼

eg:  str = 「sunck is a good man good」

print(str.encode(「utf-8」)) 返回b'sunck is a good man good'

decode()解碼

eg:  str = 「sunck is a good man good」

data = str.encode(「utf-8」))

print(data.decode(「utf-8」)) 返回sunck is a good man good

isalpha()

(若字串中至少與乙個字元且所有的字元都是字母返回true,否則返回false)

eg:str = 「sunck is a good man good」

print(str.islapha()) 返回true

isalnum()

(若字串中至少與乙個字元且所有的字元都是字母或者數字返回true,否則返回false)

eg:str = 「sunck is a good man good 56481」

print(str.isalnum()) 返回true

isupper()

(若字串中至少與乙個英文本元且所有的英文本元都是大寫的英文本母返回true,否則返回false)

eg:str = 「sunck is a good man good 56481」

print(str.isupper()) 返回false

islower()

(若字串中至少與乙個英文本元且所有的英文本元都是小寫的英文本母返回true,否則返回false)

eg:str = 「sunck is a good man good 56481」

print(str.islower()) 返回true

istitle()

(若字串標題化的(小駝峰(所有首字母大寫))返回true,否則返回false)

eg:str = 「sunck is a good man good 56481」

str1 = 「sunck good 」

print(str.istitle()) 返回false

print(str1.istitle()) 返回true

isdigit()

(若字串只包含數字字元返回true,否則返回false)

eg:str = 「sunck is a good man good 56481」

str1 = 「6165132132 」

print(str.isdigit()) 返回false

print(str1.isdigit()) 返回true

isnumeric(

(同isdigit())

eg:str = 「sunck is a good man good 56481」

str1 = 「6165132132 」

print(str.isnumeric)) 返回false

print(str1.isnumeric()) 返回true

isdecimal()

(若字串只包含十進位制字元返回true,否則返回false)

eg:str = 「sunck is a good man good 56481」

str1 = 「6165132132 」

print(str.isdecimal()) 返回false

print(str1.isdecimal()) 返回true

isspace()

(若字串只包含空格(空白字元也可以)返回true,否則返回false)

eg:str = 「     」

str1 = 「\t\n\r\f」

print(str.isspace)) 返回true

print(str1.isspacec()) 返回true

python小筆記 字串

單引號或者雙引號括起來的任意文字 建立 str sunck good 運算 字串連線 str1 sunck str2 good str3 str1 str2 print str3 str3 返回str3 sunck good 輸出重複的字串 str1 good str2 str1 3 print s...

python小筆記 字串2

轉義字元 將一些字元轉換成有特殊含義的字元 eg print sunck n good 返回sunck n good 返回單引號 eg print sunck is good 返回sunck is good eg print sunck is good 返回sunck is good 返回雙引號 e...

Python學習筆記 字串小練習

一 編寫乙個程式,接受一行序列作為輸入,並在將句子中的所有字元大寫後列印行。假設向程式提供以下輸入 hello world practice makes perfect 則輸出為 hello world practice makes perfect str1 input 請輸入乙個字串 print ...