Python Str內部功能-個人課堂筆記,課後總結
查看str內部功能,可通過和int一樣的方法,或者使用type(str)、dir(str)來查看。
1、capitalize:首字母大寫
S.capitalize() -> string
str1 = "parr" result = str1.capitalize() print(result) #返回ParrView Code
2、center(self, width, fillchar=None):內容居中,width:總長度;fillchar:空白處填充內容(默認無)
S.center(width[, fillchar]) -> string
str1 = "View Codeparr" result = str1.center(10,"*") print(result) #返回***parr***
3、count(self, sub, start=None, end=None):str的子序列個數,區分大小寫
S.count(sub[, start[, end]]) -> int
str1 = "parrAbcadb" result = str1.count("a") print(result) #返回2,表示找到兩個aView Code
4、decode(self, encoding=None, errors=None):解碼
S.decode([encoding[,errors]]) -> object
str1 = "哈哈" result = str1.encode() str2 = result.decode() print(str2) 屏幕打印:哈哈View Code
5、encode(self, encoding=None, errors=None):編碼
S.encode([encoding[,errors]]) -> object
str1 = "哈哈" result = str1.encode() print(result) #返回b‘\xe5\x93\x88\xe5\x93\x88‘View Code
6、endswith(self, suffix, start=None, end=None):受否以XX結束,返回bool型的值
S.endswith(suffix[, start[, end]]) -> bool
str1 = "parr Luck 2017" result = str1.endswith("2017") print(result) #屏幕打印TureView Code
7、expandtabs(self, tabsize=None):將Tab轉換為空格,默認一個Tab八個空格
S.expandtabs([tabsize]) -> string
str1 = "parr\thello" result = str1.expandtabs() print(result) 屏幕打印:parr helloView Code
8、find(self, sub, start=None, end=None):尋找子序列位置,如沒找到返回-1
S.find(sub [,start [,end]]) -> int
str1 = "parr" result = str1.find(‘a‘) print(result) #屏幕打印1,表示a在1位置View Code
9、isalnum(self):是否是字母或數字,返回bool型的值
S.isalnum() -> bool
str1 = "parr2017" result = str1.isalnum() print(result) #屏幕打印:TureView Code
10、isalpha(self): :是否是字母
S.isalpha() -> bool
str1 = "parr" result = str1.isalpha() print(result) #屏幕打印:TureView Code
11、isnumeric(self):是否是數字
S.isnumeric() -> bool
str1 = "2017" result = str1.isnumeric() print(result) #屏幕打印TureView Code
12、islower(self):是否是小寫
isupper(self)::是否是大寫
S.islower() -> bool
str1 = "parr" result = str1.islower() print(result) #返回Ture str1 = "parr" result = str1.isupper() print(result) #返回FalseView Code
13、replace(self, old, new, count=None):替換內容
S.replace(old, new[, count]) -> string
str1 = "parr" result = str1.replace(‘a‘,‘hello‘) print(result) #屏幕打印:phellorrView Code
14、split(self, sep=None, maxsplit=None):分割內容,maxsplit最多分割幾次,返回一個列表
S.split([sep [,maxsplit]]) -> list of strings
str1 = "parr" result = str1.split(‘a‘) print(result) #返回[‘p‘, ‘rr‘]View Code
15、splitlines(self, keepends=False)::根據換行分割,返回一個列表
S.splitlines(keepends=False) -> list of strings
str1 = """parr hello everyone """ result = str1.splitlines() print(result) #返回[‘parr‘, ‘ hello‘, ‘ everyone‘]View Code
16、startswith(self, prefix, start=None, end=None):是否起始為prefix中的內容
S.startswith(prefix[, start[, end]]) -> bool
str1 = """parr hello everyone """ result = str1.startswith(‘parr‘) print(result) #返回TureView Code
17、strip(self, chars=None):移除兩端空白
S.strip([chars]) -> string or unicode
str1 = " parr2017 " result = str1.strip() print(result) #返回parr2017View Code
18、__contains__(self, y):檢查y是否包含於x中
x.__contains__(y) <==> y in x
View CodePython Str內部功能-個人課堂筆記,課後總結