day04-作业

1.capitalize()

  • 将字符串的第一个字符转换为大写
str1 = 'abc' 
print(str1.capitalize()) # Abc

2.center(width, fillchar)

  • 返回一个指定的宽度 width居中的字符串,fillchar为填充的字符,默认为空格。
str1 = 'abc'
print(str1.center(7, '0')) # 00abc00

3.count(str)

  • 返回 str 在 string 里面出现的次数
print('abc and abc'.count('a')) # 3

4.bytes.decode(encoding="utf-8", errors="strict")

  • Python3 中没有 decode 方法,但我们可以使用 bytes 对象的 decode() 方法来解码给定的 bytes 对象,这个 bytes 对象可以由 str.encode() 来编码返回。
str = b'hello world'
str.decode(encoding='utf-8') # 'hello world'

5.encode(encoding='UTF-8',errors='strict')

  • 以 encoding 指定的编码格式编码字符串,如果出错默认报一个ValueError 的异常,除非 errors 指定的是'ignore'或者'replace'
'hello world'.encode(encoding='utf-8') # b'hello world'

6.endswith(suffix)

  • 检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 False.
'hello world'.endswith('d') # True

7.expandtabs(tabsize=8)

  • 把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 8 .
A = '       abc'
len(A) # 4
len(A.expandtabs()) # 11

8.find(str, beg=0 end=len(string))

  • 检测 str 是否包含在字符串中,如果指定范围 beg 和 end ,则检查是否包含在指定范围内,如果包含返回开始的索引值,否则返回-1
A = 'hello world'
A.find('o',6,9) # 7

9.index(str, beg=0, end=len(string))

  • 跟find()方法一样,只不过如果str不在字符串中会报一个异常.
A = 'hello world'
A.index('o',6,9) # 7

10.isalnum()

  • 如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False.
A = 'hello world'
A.isalnum() # False

11.isalpha()

  • 如果字符串至少有一个字符并且所有字符都是字母则返回 True, 否则返回 False
A = 'helloworld'
A.isalpha() # True

12.isdigit()

  • 如果字符串只包含数字则返回 True 否则返回 False.
A = '123'
A.isdigit() # True

13.islower()

  • 如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False
A = 'a123'
A.islower() # True

14.isnumeric()

  • 如果字符串中只包含数字字符,则返回 True,否则返回 False(中文数字也可以)
A = '二十'
A.isnumeric() # True

15.isspace()

  • 如果字符串中只包含空白,则返回 True,否则返回 False.
A = ' '
A.isspace() # True
A = ' abc'
A.isspace() # False 

16.istitle()

  • 如果字符串是标题化的(见 title())则返回 True,否则返回 False
A = 'Abc Def Ghi'
A.istitle() # True

17.isupper()

  • 如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False
A = 'Abc 123'
A.isupper() # False
A = 'ABC 123'
A.isupper() # True

18.join(seq)

  • 以指定字符串作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串
A = '-'
A.join('1234') '1-2-3-4'

19.len(string)

  • 返回字符串长度
A = 'ABC'
len(A) # 3

20.ljust(width[, fillchar])

  • 返回一个原字符串左对齐,并使用 fillchar 填充至长度 width 的新字符串,fillchar 默认为空格。
A = 'ABC'
A.ljust(6,'0') # ruturn 'ABC000‘

21.lower()

  • 转换字符串中所有大写字符为小写.
A = 'ABC'
A.lower() # 'abc'

22.lstrip()

  • 截掉字符串左边的空格或指定字符。
A = ' abc'
A.lstrip() # 'abc'
A = '?abc'
A.lstrip('?' # 'abc'

23.max(str)

  • 返回字符串 str 中最大的字母。
A = 'cda'
max(A) # 'd'

24.min(str)

  • 返回字符串 str 中最小的字母。
A = 'cda'
min(A) # 'a'

25.replace(old, new [, max])

  • 将字符串中的 str1 替换成 str2,如果 max 指定,则替换不超过 max 次。
A = 'abc cba abc cad'
A.replace('abc', '111', 1) # '111 cba abc cad'

26.rfind(str, beg=0,end=len(string))

  • 类似于 find()函数,不过是从右边开始查找.
A = 'abc cba abc cad'
A.rfind('abc') # 8

27.rindex( str, beg=0, end=len(string))

  • 类似于 index(),不过是从右边开始.
A = 'abc cba abc cad'
A.rindex('abc') # 8

28.rjust(width,[, fillchar]

  • 返回一个原字符串右对齐,并使用fillchar(默认空格)填充至长度 width 的新字符串
A = 'abc'
A.rjust(6,'0') # '000abc'

29.rstrip()

  • 删除字符串字符串末尾的空格.
A = 'abc    '
A.rstrip() # 'abc'

30.split(str="", num=string.count(str))

  • num=string.count(str))以 str 为分隔符截取字符串,如果 num 有指定值,则仅截取 num 个子字符串
A = 'A B C D'
A.split() # ['A', 'B', 'C', 'D']
A = 'A,B,C,D'
A.split(',',2) # ['A', 'B', 'C,D']

31.splitlines([keepends]

  • 按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
A = 'A\rB\n'
print(A)
B

A.splitlines(True)
['A\r', 'B\n']
A.splitlines(False)
['A', 'B']

32.startswith(str, beg=0,end=len(string))

  • 检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查。
A = 'ABC'
A.startswith('A') # True

33.strip([chars])

  • 在字符串上执行 lstrip()和 rstrip()
A = '   ABC   '
A.strip() # 'ABC'
A = '?ABC?'
A.strip('?') # 'ABC'

34.swapcase()

  • 将字符串中大写转换为小写,小写转换为大写
A='ABCabc'
A.swapcase() # 'abcABC'

35.title()

  • 返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())
A= 'abc cde'
A.title() # 'Abc Cde'

36.upper()

  • 转换字符串中的小写字母为大写
A = 'abc'
A.upper() # 'ABC'

37.zfill (width)

  • 返回长度为 width 的字符串,原字符串右对齐,前面填充0
A = 'ABC'
A.zfill(6) # '000ABC'

38.isdecimal()

  • 检查字符串是否只包含十进制字符
A = '\u4e00a'
A.isdecimal() # False
A = 'a'
A.isdecimal() # False
A = '10'
A.isdecimal() # True

你可能感兴趣的:(day04-作业)