`
mingren135
  • 浏览: 69078 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Python-字符串处理

 
阅读更多

目前为止,自己写的脚本中,用到最多的就是字符串处理,但只是业余时间写写脚本,因此会忘掉一些简单方法,在这里总结一下。

 

1)截取字符串:有2种方式,孰优孰劣显而易见

 

__getslice__(...)
    x.__getslice__(i, j) <==> x[i:j]

    Use of negative indices is not supported.

 

翻转字符串,x[::-1],-1是步长值

 

2)去除字符串前后空格

 

strip(...)  
    S.strip([chars]) -> string or unicode  
  
    Return a copy of the string S with leading and trailing  
    whitespace removed.  
    If chars is given and not None, remove characters in chars instead.  
    If chars is unicode, S will be converted to unicode before stripping  
  
lstrip(...)  
    S.lstrip([chars]) -> string or unicode  
  
rstrip(...)  
    S.rstrip([chars]) -> string or unicode 		
 

3)匹配字符串前、后缀

 

startswith(...)
    S.startswith(prefix[, start[, end]]) -> bool

    Return True if S starts with the specified prefix, False otherwise.
    With optional start, test S beginning at that position.
    With optional end, stop comparing S at that position.
    prefix can also be a tuple of strings to try.

endswith(...)
    S.endswith(suffix[, start[, end]]) -> bool
 

4)分隔、连接字符串

 

split(...)
    S.split([sep [,maxsplit]]) -> list of strings

    Return a list of the words in the string S, using sep as the
    delimiter string.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are removed
    from the result.

join(...)
    S.join(iterable) -> string

    Return a string which is the concatenation of the strings in the
    iterable.  The separator between elements is S.

 

5)查找字符串是否存在:还是find更习惯一些

 

index(...)
    S.index(sub [,start [,end]]) -> int

    Like S.find() but raise ValueError when the substring is not found.

find(...)
    S.find(sub [,start [,end]]) -> int

    Return the lowest index in S where substring sub is found,
    such that sub is contained within s[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.

6)tiny methods

 #是否在其中
aa = 'shanghai'
b = 'an'
print b in aa 
print b not in aa
##连接
c = 'big'
print c + aa
##复制
print c*2
##长度
print len(aa)
##最小
print min(aa)
##最大
print max(aa)
##字母所在索引
print aa.index('a')
##统计出现次数
print aa.count('a')
 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    jmu-python-字符串异常处理.txt

    jmu-python-字符串异常处理.txt

    Python内置的字符串处理函数

    Python内置的字符串处理函Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。自从20世纪90年代初Python语言诞生至今,它逐渐被广泛应用于处理系统管理任务和Web编程。

    Python_字符串处理.txt

    Python_字符串处理.txt

    Python代码-字符串转换.py

    ——学习参考资料:仅用于个人学习使用! 本代码仅作学习交流,切勿用于商业用途,否则后果自负。若涉及侵权,请联系,会尽快处理! 未进行详尽测试,请自行调试!

    py-字符串函数.docx

    在本文中,我们将介绍一些常用的Python字符串函数。 1. len()函数 len()函数用于获取字符串的长度。它返回字符串中字符的数量。例如,如果我们有一个字符串"Hello World",那么len()函数将返回11,因为这个字符串有...

    python字符串处理

    python第三章,字符串的简单处理及应用。以及数据类型

    Python代码-字符串判断方法.py

    ——学习参考资料:仅用于个人学习使用! 本代码仅作学习交流,切勿用于商业用途,否则后果自负。若涉及侵权,请联系,会尽快处理! 未进行详尽测试,请自行调试!

    Python代码-字符串的定义和下标访问.py

    ——学习参考资料:仅用于个人学习使用! 本代码仅作学习交流,切勿用于商业用途,否则后果自负。若涉及侵权,请联系,会尽快处理! 未进行详尽测试,请自行调试!

    py-字符串函数(1).docx

    在本文中,我们将介绍一些常用的Python字符串函数。 1. len()函数 len()函数用于获取字符串的长度。它返回字符串中字符的数量。例如,如果我们有一个字符串"Hello World",那么len()函数将返回11,因为这个字符串有...

    python字符串处理实例.docx

    python字符串处理实例.docx

    python源码字符串处理技术

    python源码字符串处理技术提取方式是百度网盘分享地址

    Python代码-字符串操作-分割_连接.py

    ——学习参考资料:仅用于个人学习使用! 本代码仅作学习交流,切勿用于商业用途,否则后果自负。若涉及侵权,请联系,会尽快处理! 未进行详尽测试,请自行调试!

    python常见字符串处理函数与用法汇总

    主要介绍了python常见字符串处理函数与用法,结合实例形式详细分析了Python字符串操作函数find、join、replace及split功能、使用技巧与操作注意事项,需要的朋友可以参考下

    Python-一个快速强大的Python库用于检查字符串中的敏感词

    一个快速,强大的Python库,用于检查字符串中的敏感词

    PYTHON常用字符串处理函数.txt

    PYTHON常用字符串处理函数 在历史上string类在python中经历了一段轮回的历史。在最开始的时候,python有一个专门的string的module,要使用string的方法要先import,但后来由于众多的python使用者的建议,从python2.0...

    Python字符串处理.md

    Python字符串处理,字符串的查询字符串的统计字符串的分割一刀两段式切割字符串的对齐方式。。。。。

    python中字符串方法.docx

    python中字符串方法 Python中的字符串方法是非常强大的,它们可以帮助我们处理和操作字符串。在本文中,我们将介绍一些常用的字符串方法,包括字符串的拼接、替换、查找、分割、大小写转换等。 1. 字符串的拼接 字符...

    PYTHON常用字符串处理函数.doc

    Python

    字符串处理-使用Python逆序输出字符串.zip

    字符串处理-使用Python逆序输出字符串.zip

    Python代码-字符串操作-去除空白字符.py

    ——学习参考资料:仅用于个人学习使用! 本代码仅作学习交流,切勿用于商业用途,否则后果自负。若涉及侵权,请联系,会尽快处理! 未进行详尽测试,请自行调试!

Global site tag (gtag.js) - Google Analytics