[Python] Python常用内置的字符串处理函数整理 →→→→→进入此内容的聊天室

来自 , 2020-12-19, 写在 Python, 查看 111 次.
URL http://www.code666.cn/view/3ab6be46
  1. 收集常用的Python 内置的各种字符串处理 函数的使用方法
  2.  
  3. str='python String function'
  4.  
  5. 生成字符串变量str='python String function'
  6.  
  7. 字符串长度获取:len(str)
  8. 例:print '%s length=%d' % (str,len(str))
  9.  
  10. 字母处理
  11. 全部大写:str.upper()
  12. 全部小写:str.lower()
  13. 大小写互换:str.swapcase()
  14. 首字母大写,其余小写:str.capitalize()
  15. 首字母大写:str.title()
  16. print '%s lower=%s' % (str,str.lower())
  17. print '%s upper=%s' % (str,str.upper())
  18. print '%s swapcase=%s' % (str,str.swapcase())
  19. print '%s capitalize=%s' % (str,str.capitalize())
  20. print '%s title=%s' % (str,str.title())
  21. 格式化相关
  22. 获取固定长度,右对齐,左边不够用空格补齐:str.ljust(width)
  23. 获取固定长度,左对齐,右边不够用空格补齐:str.ljust(width)
  24. 获取固定长度,中间对齐,两边不够用空格补齐:str.ljust(width)
  25. 获取固定长度,右对齐,左边不足用0补齐
  26. print '%s ljust=%s' % (str,str.ljust(20))
  27. print '%s rjust=%s' % (str,str.rjust(20))
  28. print '%s center=%s' % (str,str.center(20))
  29. print '%s zfill=%s' % (str,str.zfill(20))
  30.  
  31. 字符串搜索相关
  32. 搜索指定字符串,没有返回-1str.find('t')
  33. 指定起始位置搜索:str.find('t',start)
  34. 指定起始及结束位置搜索:str.find('t',start,end)
  35. 从右边开始查找:str.rfind('t')
  36. 搜索到多少个指定字符串:str.count('t')
  37. 上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1
  38. print '%s find nono=%d' % (str,str.find('nono'))
  39. print '%s find t=%d' % (str,str.find('t'))
  40. print '%s find t from %d=%d' % (str,1,str.find('t',1))
  41. print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2))
  42. #print '%s index nono ' % (str,str.index('nono',1,2))
  43. print '%s rfind t=%d' % (str,str.rfind('t'))
  44. print '%s count t=%d' % (str,str.count('t'))
  45.  
  46. 字符串替换相关
  47. 替换old为newstr.replace('old','new')
  48. 替换指定次数的old为newstr.replace('old','new',maxReplaceTimes)
  49. print '%s replace t to *=%s' % (str,str.replace('t', '*'))
  50. print '%s replace t to *=%s' % (str,str.replace('t', '*',1))
  51.  
  52. 字符串去空格及去指定字符
  53. 去两边空格:str.strip()
  54. 去左空格:str.lstrip()
  55. 去右空格:str.rstrip()
  56. 去两边字符串:str.strip('d'),相应的也有lstrip,rstrip
  57. str=' python String function '
  58. print '%s strip=%s' % (str,str.strip())
  59. str='python String function'
  60. print '%s strip=%s' % (str,str.strip('d'))
  61.  
  62. 按指定字符分割字符串为数组:str.split(' ')
  63. 默认按空格分隔
  64. str='a b c de'
  65. print '%s strip=%s' % (str,str.split())
  66. str='a-b-c-de'
  67. print '%s strip=%s' % (str,str.split('-'))
  68.  
  69. 字符串判断相关
  70. 是否以start开头:str.startswith('start')
  71. 是否以end结尾:str.endswith('end')
  72. 是否全为字母或数字:str.isalnum()
  73. 是否全字母:str.isalpha()
  74. 是否全数字:str.isdigit()
  75. 是否全小写:str.islower()
  76. 是否全大写:str.isupper()
  77. str='python String function'
  78. print '%s startwith t=%s' % (str,str.startswith('t'))
  79. print '%s endwith d=%s' % (str,str.endswith('d'))
  80. print '%s isalnum=%s' % (str,str.isalnum())
  81. str='pythonStringfunction'
  82. print '%s isalnum=%s' % (str,str.isalnum())
  83. print '%s isalpha=%s' % (str,str.isalpha())
  84. print '%s isupper=%s' % (str,str.isupper())
  85. print '%s islower=%s' % (str,str.islower())
  86. print '%s isdigit=%s' % (str,str.isdigit())
  87. str='3423'
  88. print '%s isdigit=%s' % (str,str.isdigit())
  89. #//python/6713

回复 "Python常用内置的字符串处理函数整理"

这儿你可以回复上面这条便签

captcha