鱼C论坛

 找回密码
 立即注册
查看: 1914|回复: 0

[技术交流] 《零基础学习Python》14 字符串:各种奇葩的内置方法

[复制链接]
发表于 2017-7-23 20:26:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 只为 于 2017-8-8 21:57 编辑

  1. 字符串:一旦定义不能修改。


  2. 1、capitalize()

  3. >>> str2 = 'xiaoxie'

  4. >>> str2.capitalize()

  5. 'Xiaoxie'

  6. 注意:返回一个新字符串,原字符串不变。


  7. 2、casefold()

  8. >>> str2 = 'DAXIExiaoxie'

  9. >>> str2.casefold()

  10. 'daxiexiaoxie'

  11. 注意:返回一个新字符串,原字符串不变。


  12. 3、center(width)

  13. >>> str2.center(40)

  14. '              DAXIExiaoxie              '


  15. 4、count(sub[,start][,end])

  16. >>> str2.count('xi')

  17. 2


  18. 5、encode(encoding='utf-8',errors='strict')
  19.        



  20. 6、endswith(sub[,start][,end])

  21. >>> str2.endswith('e')

  22. True

  23. 7、startswidth(prefix[,start][,end])




  24. 8、expandtabs([tabsize=8])

  25. >>> str2.expandtabs()

  26. 'I       love    fishc.com!'

  27. >>> str2.expandtabs(4)

  28. 'I   love    fishc.com!'


  29. 9、find(sub[,start][,end])

  30. >>> str2.find('efc')

  31. -1

  32. >>> str2.find('com')

  33. 13


  34. 10、rfind(sub[,start][,end])
  35. >>> str2 = 'xixixi'
  36. >>> str2.rfind('x')
  37. 4
  38.                                
  39. 11、index(sub[,start][,end])
  40. >>> str2.index('x')
  41. 0
  42.        
  43. 12、rindex(sub[,start][,end])
  44. >>> str2.rindex('x')
  45. 4


  46. 12、isalnum()

  47. >>> str3 = '9ik'
  48. >>> str4 = '9908'
  49. >>> str5 = 'ldlld'
  50. >>> str6 = 'kjkf&$#@'
  51. >>> str3.isalnum()
  52. True
  53. >>> str4.isalnum()
  54. True
  55. >>> str5.isalnum()
  56. True
  57. >>> str6.isalnum()
  58. False

  59. 13、isalpha()
  60. >>> str3.isalpha()
  61. False
  62. >>> str4.isalpha()
  63. False
  64. >>> str5.isalpha()
  65. True
  66. >>> str6.isalpha()
  67. False

  68. 14、isdecimal()

  69. >>> str4.isdecimal()
  70. Traceback (most recent call last):
  71. File "<stdin>", line 1, in <module>
  72. AttributeError: 'str' object has no attribute 'isdecimal'

  73. 注意:python2没有此方法的,还有python3中不知道如果str是个float类型的会是什么结果
  74. 15、isdigit()
  75.        
  76. 16、isnumeric()
  77.                
  78. python中str函数isdigit、isdecimal、isnumeric的区别


  79. isdigit()  True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字 False: 汉字数字  Error: 无 isdecimal() True: Unicode数字,,全角数字(双字节)  False: 罗马数字,汉字数字  Error: byte数字(单字节)  isnumeric() True: Unicode数字,全角数字(双字节),罗马数字,汉字数字  False: 无 Error: byte数字(单字节)
  80.        

  81. 17、islower()
  82. >>> str3 = '小甲鱼'

  83. >>> str3.islower()

  84. False

  85. >>> str3.isupper()

  86. False

  87. 21、isupper()
  88. >>> str3 = '小甲鱼'
  89. >>> str3.islower()
  90. False
  91. >>> str3.isupper()
  92. False


  93. 18、isspace()


  94.        
  95. 19、istitle()

  96. >>> str5 = 'FishC'

  97. >>> str5.istitle()

  98. False

  99. >>> str5='Fishc'

  100. >>> str5.istitle()

  101. True


  102. 20、title()



  103. 22、join(sub)


  104. 23、ljust(width)
  105.                                                

  106. 24、rjust(width)
  107.                                                        

  108. 25、zfill(width)
  109.                                                                                        
  110.                                                
  111. 26、lower()
  112.                                                                                        

  113. 27、upper()



  114. 28、strip([chars])
  115.                                                                                        

  116. 29、lstrip()

  117.                                                                                                                
  118. 30、rstrip()

  119. 31、partition(sub)
  120. 32、rpartition(sub)

  121.                                                                                                                                
  122. 33、replace(old,new[,count])



  123. 34、split(sep=None,maxsplit=-1)
  124. splitlines(([keepends]))

  125.                                                                        
  126. 35、swapcase()
  127.                                                                        
  128. 36、translate(table)                                                                                       

复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 11:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表