Python Str内部功能-个人课堂笔记,课后总结

查看str内部功能,可通过和int一样的方法,或者使用help(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
posted @ 2017-11-03 17:00  是梦别喊疼、  阅读(188)  评论(0编辑  收藏  举报