python String操作總結

2021-09-24 04:16:33 字數 1694 閱讀 9904

def

split

(self, sep=

none

, maxsplit=

none

):按照指定字元切割字串,返回乙個列表,可以指定切割次數

defstrip

(self, chars=

none

):去空格,去掉字串兩邊的空格

defupper

(self):轉換為大寫

deflower

(self):轉換為小寫

defreplace

(self, old, new, count=

none

):替換,將old替換為new返回新字串

defstartswith

(self, prefix, start=

none

, end=

none

):判斷是不是以字元開頭

defendswith

(self, suffix, start=

none

, end=

none

):判斷是不是以字元結尾

deffind

(self, sub, start=

none

, end=

none

):查詢子串的位置,找不到不報錯

defindex

(self, sub, start=

none

, end=

none

):查詢子串的位置,找不到則報錯

deftitle

(self):將單詞的首字母大寫

defcapitalize

(self):將字串的首字元大寫

defswapcase

(self):將字元的大小寫轉換

defjoin

(self, iterable):將可迭代物件按照字元來連線

defrjust

(self, width, fillchar=

none

):預設寬度填充,字元往右靠,填充字元向左邊填充

defljust

(self, width, fillchar=

none

):預設寬度填充,字元往左靠,填充字元向右邊填充

re model

defsplit

(pattern, string, maxsplit=

0, flags=

0):根據正則匹配切割字串

deffindall

(pattern, string, flags=

0):根據正則匹配查詢字元

defsearch

(pattern, string, flags=

0):從開始位置找子串

defmatch

(pattern, string, flags=

0):從開始位置找,必須從頭匹配

defsub

(pattern, repl, string, count=

0, flags=

0):將正則匹配到的字元替換為所需的字元

defsubn

(pattern, repl, string, count=

0, flags=

0):將正則匹配到的字元替換為所需的字元

Python String字串操作

1 name xioer pipo 2 3print name.capitalize 第乙個字元大寫 4print name.expandtabs 5print name.count o 統計重複資料 6print name.endswith 最後是否為這個字元 7print name name.f...

Python String字串操作

1 name xioer pipo 2 3print name.capitalize 第乙個字元大寫 4print name.expandtabs 5print name.count o 統計重複資料 6print name.endswith 最後是否為這個字元 7print name name.f...

Python String型別詳解

在python中,string是代表unicode字元的位元組陣列。但是在python中沒有單個的字元資料型別,a 這種只是長度為1的string 1.建立string 在python中建立字串可以用單引號,雙引號甚至是三引號。a ada b dsfsg c dasfdf a ada b dsfsg...