1. 程式人生 > >Python Str內部功能-個人課堂筆記,課後總結

Python Str內部功能-個人課堂筆記,課後總結

isl fin replace strip() ngs gif 一個 result 檢查

查看str內部功能,可通過和int一樣的方法,或者使用type(str)、dir(str)來查看。

1、capitalize:首字母大寫

  S.capitalize() -> string

技術分享
str1 = "parr"
result = str1.capitalize()
print(result)

#返回Parr
View Code

2、center(self, width, fillchar=None):內容居中,width:總長度;fillchar:空白處填充內容(默認無)

  S.center(width[, fillchar]) -> string

技術分享
str1 = "
parr" result = str1.center(10,"*") print(result) #返回***parr***
View Code

3、count(self, sub, start=None, end=None):str的子序列個數,區分大小寫

  S.count(sub[, start[, end]]) -> int

技術分享
str1 = "parrAbcadb"
result = str1.count("a")
print(result)

#返回2,表示找到兩個a
View 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)

#屏幕打印Ture
View Code

7、expandtabs(self, tabsize=None):將Tab轉換為空格,默認一個Tab八個空格

  S.expandtabs([tabsize]) -> string

技術分享
str1 = "parr\thello"
result = str1.expandtabs()
print(result)

屏幕打印:parr    hello
View 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)

#屏幕打印:Ture
View Code

10、isalpha(self): :是否是字母

  S.isalpha() -> bool

技術分享
str1 = "parr"
result = str1.isalpha()
print(result)

#屏幕打印:Ture
View Code

11、isnumeric(self):是否是數字

  S.isnumeric() -> bool

技術分享
str1 = "2017"
result = str1.isnumeric()
print(result)

#屏幕打印Ture
View Code

12、islower(self):是否是小寫

  isupper(self)::是否是大寫

  S.islower() -> bool

技術分享
str1 = "parr"
result = str1.islower()
print(result)

#返回Ture

str1 = "parr"
result = str1.isupper()
print(result)

#返回False
View Code

13、replace(self, old, new, count=None):替換內容

  S.replace(old, new[, count]) -> string

技術分享
str1 = "parr"
result = str1.replace(a,hello)
print(result)

#屏幕打印:phellorr
View 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)

#返回Ture
View Code

17、strip(self, chars=None):移除兩端空白

  S.strip([chars]) -> string or unicode

技術分享
str1 = "            parr2017              "
result = str1.strip()
print(result)

#返回parr2017
View Code

18、__contains__(self, y):檢查y是否包含於x中

  x.__contains__(y) <==> y in x

技術分享View Code

Python Str內部功能-個人課堂筆記,課後總結