首页 > 技术文章 > str内部方法释义

lwp-king666 2018-01-18 13:28 原文

1. __add__:字符串拼接

【示例】
>>> str1=‘good’
>>> str1.__add__(‘morning’)
>>> ‘goodmorning’

2. __contains__:判断字符串中是否包含某字符,是则返回True

【示例】:
>>> str1='good'
>>> str1.__contains__('od')
>>> True
>>> str1.__contains__('ods')
>>> False

3. __eq__:判断二者是否相等,是则返回True

【示例】:
>>> str1='good'
>>> str1.__eq__('good')
>>> True
>>> str1.__eq__('goods')
>>> False

4. __ge__:判断self>=value,是则返回True

【示例】:
>>> str1='s'
>>> str1.__ge__('t')
>>> False
>>> str1.__ge__('a')
>>> True

 5. __gt__:判断self>value,是则返回True

6. __le__:判断self<=value,是则返回True

7. __lt__:判断self<value,是则返回True

8. capitalize():将字符串首字母转换成大写

【示例】:
>>> str1='good'
>>> str1.capitalize()
>>> 'Good'

 9. casefold():将字符串首字母转换成小写

【示例】:
>>> str1='Good'
>>> str1.casefold()
>>> 'good'

10. center():居中显示,其余用指定字符填充,参数1:字符总长度,参数2:填充字符

【示例】:
>>> str1='good'
>>> str1.center(20,'*')
>>> '********good********'

11. count():统计字符个数

【示例】:
>>> str1='good good study,day day up'
>>> str1.count('d')
>>> 5
>>> str1.count('d',0,12)   #空格也占用1位
>>> 2

12. encode():转换编码格式

【示例】:
>>> str1='安全'
>>> str1.encode('gbk')             #由utf-8转换成gbk编码格式
>>> b'\xb0\xb2\xc8\xab'  

13. endswith():判断结尾是否是否正确,返回bool值

【示例】:
>>> str1='good good study,day day up'
>>> str1.endswith('dy',0,15)
>>> True
>>> str1.endswith('ups')
>>> False

14. expandtabs():把字符串中的tab符号(\t)转换成空格,\t默认是8个空格,可以指定空格长度

【示例】:
>>> str1='good\tup'
>>> str1.expandtabs()
>>> 'good    up'                  #从头开始计数,默认8位,不足用空格填充
>>> len(str1.expandtabs())
>>> 10
>>>  str1.expandtabs(10)          #从头开始计数,指定10位,不足用空格填充
>>> 'good      up'
>>> len(str1.expandtabs(10))    
>>> 12

15. find():返回字符串索引位置

【示例】:
>>> str1='good morning'
>>> str1.find('od')
>>> 2
>>> str1.find('o',0,2)   
>>> 1
>>> str1.find('o',0,3)   #str1[1]和str1[2]值均为'o',返回第一个'o'的索引位置
>>> 1

16. index():返回字符串索引位置,类似于find()

区别:find()查找不到指定字符串时,会返回None,而index()会报错

17. format():字符串格式化

【示例1】:
>>> str1='good {0} {1}'
>>> str1.format('day','everyone')
>>> 'good day everyone'

【示例2】:
>>> str1='Tom  is  a {sex}'
>>> str1.format(sex='boy')
>>> 'Tom is a boy'

18. isalnum():判断是否为字母或数字,返回bool值

【示例1】
>>> str1='helloworld'
>>> str1.isalnum()
>>> True

【示例2】
>>> str2=‘123>>> str2.isalnum()
>>> True

【示例3】
>>> str3='hello    world'
>>> str3.isalnum()
>>> False

19. isalpha():判断是否全为字母,返回bool值

【示例1】:
>>> str1='helloworld'
>>> str1.isalpha()
>>> True

【示例2】:
>>> str2='hello1'
>>> str2.isalpha()
>>> False

20. isdecimal():判断是否全为十进制字符,返回bool值

【示例1】:
>>> str1=‘1234567890>>> str1.isdecimal()
>>> True

【示例2】:
>>> str2='a123'
>>> str2.isdecimal()
>>> False

21. isdigit():判断是否全为数字,返回bool值

【示例1】:
>>> str1=‘123>>> str1.isdigit()
>>> True

【示例2】:
>>> str2=' '
>>> str2.isdigit()
>>> False

 22. islower():判断是否全为小写字母,返回bool值

【示例1】
>>> str1='abc'
>>> str1.islower()
>>> True

【示例2】
>>> str2='Good'
>>> str2.islower()
>>> False

 23. isnumeric():判断是否全为数字,返回bool值

【示例1】
>>> str1='123'
>>> str1.isnumeric()
>>> True

【示例2】
>>> str2='123a'
>>> str2.isnumeric()
>>> False

 24. isprintable():判断是否只包含可打印字符,返回bool值

【示例1】
>>> str1='hello world@123'
>>> str1.isprintable()
>>> True

【示例2】
>>> str2='abc\t124'
>>> str2.isprintable()
>>> False

25. isspace():判断是否只包含空格字符,返回bool值

【示例1】
>>> str1='  '
>>> str1.isspace()
>>> True

【示例2】
>>> str2='a  '
>>> str2.isspace()
>>> False

26. istitle():判断是否每个词的首字母均为大写,返回bool值

【示例1】
>>> str1='Hello World!'
>>> str1.istitle()
>>> True

【示例2】
>>> str2='Hello world'
>>> str2.istitle()
>>> False

27. isupper():判断字符串是否全为大写字母,返回bool值

【示例1】
>>> str1='GOOD'
>>> str1.isupper()
>>> True

【示例2】
>>> str2='GOOd'
>>> str2.isupper()
>>> False

28. join():将字符串分割成单个字符,且用指定字符进行拼接,并返回新的字符串

【示例】
>>> str1='helloworld'
>>> '_'.join(str1)
>>> 'h_e_l_l_o_w_o_r_l_d' 

29. ljust():左对齐,使用指定字符进行填充,默认使用空格填充

【示例1】
>>> str1='hello'
>>> str1.ljust(10)
>>> 'hello     '

【示例2】
>>> str2='hello'
>>> str2.ljust(10,'*')
>>> 'hello*****'

30. rjust():右对齐,使用指定字符填充,默认使用空格填充

【示例】
>>> str1='hello'
>>> str1.rjust(10,'*')
>>> '*****hello'

31. zfill():右对齐,若指定长度<=字符串自身长度,则返回字符串自身,反之,默认使用0填充

【示例】
>>> str1='hello'
>>> str1.zfill(10)
>>> '00000hello'

32. lower():将大写字母全部转换成小写

【示例】
>>> str1='HELLO'
>>> str1.lower()
>>> 'hello'

 33. lstrip():清除字符左边的空格

 

【示例】
>>> str1='   hello   '
>>> str1.lstrip()
>>> 'hello   '

34. rstrip():清除字符串右边的空格

>>> str1='   hello   '
>>> str1.rstrip()
>>> '   hello'

35. strip():清除字符串两边的空格

【示例】
>>> str1='   hello   '
>>> str1.strip()
>>> 'hello'

36. maketrans():与translate配合使用,对应替换相应字符

【示例】
>>> str1='this is a testing example,very interesting'
>>> test1='aeios'
>>> test2='12345'
>>> test3=str1.maketrans(test1,test2)
>>> str1.translate(test3)
>>> 'th35 35 1 t25t3ng 2x1mpl2,v2ry 3nt2r25t3ng'

37. partition():使用指定字符分割字符串,返回一个由3个元素组成的元组,3个元素分别是指定字符前后字符串以及指定字符本身

若指定字符不存在于字符串中,则返回原字符串及2个空元素

【示例1】
>>> str1='hello world! go on'
>>> str1.partition('d')
>>> ('hello worl','d','! go on')

【示例2】
>>> str1='hello world! go on'
>>> str1.partition('z')
>>> ('hello world! go on','','')

38. rpartition():使用指定字符从右分割字符串,返回一个由3个元素组成的元组,3个元素分别是指定字符前后字符串以及指定字符本身,与partition()相反

若指定字符不存在于字符串中,则返回原字符串及2个空元素

【示例1】
>>> str1='hello world! go on'
>>> str1.rpartition('o')
>>> ('hello world! go ','o','n')

【示例2】
>>> str1='hello world! go on'
>>> str1.rpartition('z')
>>> ('','','hello world! go on')

39. replace():使用新字符替换旧字符,可指定替换个数,默认替换全部

【示例1】
>>> str1='hello world! go on'
>>> str1.replace('o','8',2)
>>> 'hell8 w8rld! go on'

40. rfind():从字符串右侧开始查找,返回字符索引位置,与find()相反

【示例】
>>> str1='hello world! go on'
>>> str1.rfind('o')
>>> 16

41. rindex():从字符串右侧开始查找,返回字符索引位置,与index()相反,类似rfind()

42. split():从左侧拆分字符串,返回一个列表类型

1:默认分隔符为空格:

【示例1】
#默认分割符为空格
>>> str1='hello world! go on'
>>> str1.split()
>>> ['hello','world!','go','on']

2: 分隔符不能为空('')

3:若没有分割符,则将整个字符串作为列表的一个元素输出

【示例】
>>> str1='helloworld'
>>> str1.split()
>>>['helloworld']

4: 指定分割次数

【示例】
>>> str1='hello world! go on'
>>> str1.split(' ','2')           #使用空格进行分割,分割次数为2,则返回N+1即3个元素
>>> ['hello','world!','go on']

 5. 特殊情况:出现多个分割符连接在一起

分割思路:分隔符分割字符串时,分割符为x,则将x左侧的字符串放入列表中,若x左侧除x外无其他字符串,则返回空''作为列表的一个元素,继续分割x右侧剩余的字符串,以此类推

第一次分割------>['','xhelloxxxworldxxxxgox']

第二次分割------>['','','helloxxxworldxxxxgox']

第三次分割------>['','','hello','xxworldxxxxgox']

第四次分割------>['','','hello','','xworldxxxxgox']............以此类推

 43. rsplit():从右侧开始分割,与split()相反

【示例】
>>> str1='hello world! go on'
>>> str1.rsplit('g')
>>> ['helloworld! ','o on']

 44. splitlines():分割多行字符串,将每行字符串作为列表的一个元素

 keepends 值决定输出结果中是否保留换行符,默认为 False,不包含换行符,若为 True,则保留换行符

 1. 1. 多行字符串,默认分割

 2. keepends值为True,进行分割

 45. startswith():判断开头字符是否正确,返回bool值

【示例1】
>>> str1='hello world! go on'
>>> str1.startswith('he')
>>> True


【示例2】
>>> str1='hello world! go on'
>>> str1.startswith('wo',0,6)
>>> True

46.  swapcase():将字符串中的大小写字母互转

【示例】
>>> str1='Hello World'
>>> str1.swapcase()
>>> 'hELLO wORLD'

47. title():字符串中每个单词首字母大写

【示例】
>>> str1='hello world! go on'
>>> str1.title()
>>> 'Hello World! Go On'

 48.upper():将字符串全转换为大写字母,与lower()相反

【示例】
>>> str1='hello world!'
>>> str1.upper()
>>> 'HELLO WORLD!'

 49.  __hash__:如果对象object为哈希表类型,返回对象object的哈希值。哈希值为整数。在字典查找中,哈希值用于快速比较字典的键。两个数值如果相等,则哈希值也相等,等于hash(values)

50. __init__:str构造方法,操作a='s' or  a=str('s')自动调用

51. __len__:返回字符串长度

【示例】:
>>> str1='student'
>>> str1.__len__()
>>> 7

 52. __getattribute__:反射会用到

 

推荐阅读