Python字符串操作

转自:http://blog.chinaunix.net/uid-25992400-id-3283846.html 

 任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作。

    python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求:
  • python的字符串属性函数
  • python的string模块
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1. 字符串属性函数  
     系统版本:CentOS release 6.2 (Final)2.6.32-220.el6.x86_64
     python版本:Python 2.6.6

字符串属性方法

  1. >>> str='string learn'
  2. >>> dir(str)
  3. ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
可以将这些方法按功能用途划分为以下几种类型:

字符串格式输出对齐

  1. >>> str='stRINg lEArn'
  2. >>>
  3. >>> str.center(20)      #生成20个字符长度,str排中间
  4. '    stRINg lEArn    '
  5. >>> 
  6. >>> str.ljust(20)       #str左对齐
  7. 'stRINg lEArn        '  
  8. >>>
  9. >>> str.rjust(20)       #str右对齐
  10. '        stRINg lEArn'
  11. >>> 
  12. >>> str.zfill(20)       #str右对齐,左边填充0
  13. '00000000stRINg lEArn'

大小写转换

  1. >>> str='stRINg lEArn' 
  2. >>> 
  3. >>> str.upper() #转大写
  4. 'STRING LEARN'
  5. >>> 
  6. >>> str.lower() #转小写
  7. 'string learn'
  8. >>> 
  9. >>> str.capitalize() #字符串首为大写,其余小写
  10. 'String learn'
  11. >>> 
  12. >>> str.swapcase() #大小写对换
  13. 'STrinG LeaRN'
  14. >>> 
  15. >>> str.title() #以分隔符为标记,首字符为大写,其余为小写
  16. 'String Learn'

字符串条件判断

  1. >>> str='0123'
  2. >>> str.isalnum( #是否全是字母和数字,并至少有一个字符 
  3. True
  4. >>> str.isdigit()   #是否全是数字,并至少有一个字符
  5. True

  6. >>> str='abcd'
  7. >>> str.isalnum()
  8. True
  9. >>> str.isalpha()   #是否全是字母,并至少有一个字符
  10. True
  11. >>> str.islower()   #是否全是小写,当全是小写和数字一起时候,也判断为True 
  12. True

  13. >>> str='abcd0123'
  14. >>> str.islower()   #同上
  15. True
  16. >>> str.isalnum()   
  17. True

  18. >>> str=' '
  19. >>> str.isspace()    #是否全是空白字符,并至少有一个字符
  20. True
  21. >>> str='ABC'
  22. >>> str.isupper()    #是否全是大写,当全是大写和数字一起时候,也判断为True
  23. True
  24. >>> str='Abb Acc'
  25. >>> str.istitle()    #所有单词字首都是大写,标题
  26. True

  27. >>> str='string learn'
  28. >>> str.startswith('str') #判断字符串以'str'开头
  29. True
  30. >>> str.endswith('arn')   #判读字符串以'arn'结尾
  31. True

字符串搜索定位与替换

  1. >>> str='string lEARn'
  2. >>> 
  3. >>> str.find('a')      #查找字符串,没有则返回-1,有则返回查到到第一个匹配的索引
  4. -1
  5. >>> str.find('n')
  6. 4
  7. >>> str.rfind('n')     #同上,只是返回的索引是最后一次匹配的
  8. 11
  9. >>> 
  10. >>> str.index('a')     #如果没有匹配则报错
  11. Traceback (most recent call last):
  12.   File "<stdin>", line 1, in <module>
  13. ValueError: substring not found
  14. >>> str.index('n')     #同find类似,返回第一次匹配的索引值
  15. 4
  16. >>> str.rindex('n')    #返回最后一次匹配的索引值
  17. 11
  18. >>>
  19. >>> str.count('a')     #字符串中匹配的次数
  20. 0
  21. >>> str.count('n')     #同上
  22. 2
  23. >>>
  24. >>> str.replace('EAR','ear')  #匹配替换
  25. 'string learn'
  26. >>> str.replace('n','N')
  27. 'striNg lEARN'
  28. >>> str.replace('n','N',1)
  29. 'striNg lEARn'
  30. >>>
  31. >>>
  32. >>> str.strip('n')   #删除字符串首尾匹配的字符,通常用于默认删除回车符
  33. 'string lEAR'
  34. >>> str.lstrip('n')  #左匹配
  35. 'string lEARn'
  36. >>> str.rstrip('n')  #右匹配
  37. 'string lEAR'
  38. >>>
  39. >>> str=' tab'
  40. >>> str.expandtabs()  #把制表符转为空格
  41. '      tab'
  42. >>> str.expandtabs(2) #指定空格数
  43. ' tab'

字符串编码与解码

  1. >>> str='字符串学习'
  2. >>> str
  3. '\xe5\xad\x97\xe7\xac\xa6\xe4\xb8\xb2\xe5\xad\xa6\xe4\xb9\xa0'
  4. >>> 
  5. >>> str.decode('utf-8')               #解码过程,将utf-8解码为unicode
  6. u'\u5b57\u7b26\u4e32\u5b66\u4e60'

  7. >>> str.decode('utf-8').encode('gbk')  #编码过程,将unicode编码为gbk
  8. '\xd7\xd6\xb7\xfb\xb4\xae\xd1\xa7\xcf\xb0'
  9. >>> str.decode('utf-8').encode('utf-8')  #将unicode编码为utf-8
  10. '\xe5\xad\x97\xe7\xac\xa6\xe4\xb8\xb2\xe5\xad\xa6\xe4\xb9\xa0'

字符串分割变换

  1. >>> str='Learn string'
  2. >>> '-'.join(str)
  3. 'L-e-a-r-n- -s-t-r-i-n-g'
  4. >>> l1=['Learn','string']
  5. >>> '-'.join(l1)
  6. 'Learn-string'
  7. >>> 
  8. >>> str.split('n')
  9. ['Lear', ' stri', 'g']
  10. >>> str.split('n',1)
  11. ['Lear', ' string']
  12. >>> str.rsplit('n',1)
  13. ['Learn stri', 'g']
  14. >>>
  15. >>> str.splitlines()
  16. ['Learn string']
  17. >>>
  18. >>> str.partition('n')
  19. ('Lear', 'n', ' string')
  20. >>> str.rpartition('n')
  21. ('Learn stri', 'n', 'g')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值