zoukankan      html  css  js  c++  java
  • Python 常见的字符串操作

    1、strip、lstrip和rstrip

    描述:

    用于移除字符串左右两边、左边、右边指定的字符(默认为空白符,例如:/n, /r, /t, ' ')或字符序列。

    语法:

    str.strip([chars])
    str.lstrip([chars])
    str.rstrip([chars])

    例如:

    1)移除单个字符或空白:

    >>> '  abc '.strip()
    'abc'
    
    >>> '  abc'.lstrip()
    'abc'
    
    >>> 'abc  '.rstrip()
    'abc'
    
    >>> 'abc'.strip('a')
    'bc'
    
    >>> 'abc'.lstrip('a')
    'bc'
    
    >>> 'abc'.rstrip('c')
    'ab'
    View Code

    2)移除一个字符串列表(是否会删除的前提是从字符串最开头和最结尾是不是包含要删除的字符,如果有就会继续处理,没有的话是不会删除中间的字符的):

    >>> 'abc@163.com'.strip('cawm')
    'bc@163.co'
    
    >>> 'abc@163.com'.lstrip('cawm')
    'bc@163.com'
    
    >>> 'abc@163.com'.rstrip('cawm')
    'abc@163.co'
    View Code

     2、大小写转换lower、upper、title、capitalize、swapcase

    描述:

    lower:将字符串中的大写字母转为小写字母。

    upper:将字符串中的小写字母转为大写字母。

    title:将所有单词首字母转为大写,其余字母均转为小写。

    capitalize:将字符串的第一个字母转为大写,其他字母转为小写。

    swapcase:将字符串做大小写字母转换(大写->小写,小写->大写)

    语法:

    str.lower()
    str.upper()
    str.title()
    str.capitalize()
    str.swapcase()

    例如:

    >>> 'aBcDe'.upper()
    'ABCDE'
    
    >>> 'aBcDe'.lower()
    'abcde'
    
    >>> 'thIs is a exaMple'.title()
    'This Is A Example'
    
    >>> 'this is A example'.capitalize()
    'This is a example'
    
    >>> 'aBcDe'.swapcase()
    'AbCdE'
    View Code

     3、find、index、rfind、rindex

    描述:

    find:检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。

    index:检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。

    rfind:类似于find()函数,只不过是从字符串右边开始查找。

    rindex:类似于index()函数,只不过是从字符串右边开始查找。

    语法:

    str.find(str, beg=0, end=len(string))
    str.index(str, beg=0, end=len(string))
    • str -- 指定检索的字符串
    • beg -- 开始索引,默认为0。
    • end -- 结束索引,默认为字符串的长度。

    例如:

    >>> str1 = 'This is a example!'
    ... str2 = 'example'
    ... index = str1.find(str2, 0, len(str1))
    ... print(index)
    10
    
    >>> str1 = 'This is a example!'
    ... str2 = 'example'
    ... index = str1.index(str2, 0, len(str1))
    ... print(index)
    10
    View Code

     4、count

    描述:

    统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

    语法:

    str.count(sub, start= 0, end=len(string))

    例如: 

    >>> 'hello world'.count('o')
    2
    
    >>> 'hello world'.count('o', 5 , len('hello world'))
    1
    View Code

     5、replace

    描述:

    把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

    语法:

    str.replace(old, new[, max])
    • old -- 将被替换的子字符串。
    • new -- 新字符串,用于替换old子字符串。
    • max -- 可选字符串, 替换不超过 max 次。

    例如:

    >>> str = 'This is a example'
    ... nStr = str.replace('is', 'was')
    ... print(nStr)
    Thwas was a example
    
    >>> str = 'This is a example'
    ... nStr = str.replace('a', 'some', 1)
    ... print(nStr)
    This is some example
    View Code

     6、split、splitlines、partition、rpartition

    描述:

    split:指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串。

    splitlines:按照行(' ', ' ', ')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。

    partition:根据指定的分隔符将字符串进行分割。如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。

    rpartition:类似于partition()函数,只不过是从右边开始。

    语法:

    str.split(str="", num=string.count(str))
    • str -- 分隔符,默认为所有的空字符,包括空格、换行( )、制表符( )等。
    • num -- 分割次数。
    str.splitlines([keepends])
    • keepends -- 在输出结果里是否保留换行符(' ', ' ', '),默认为 False,不包含换行符,如果为 True,则保留换行符。
    str.partition(str)
    • str-- 指定的分隔符。
    str.rpartition(str)

    例如:

    >>> 'This 
    is a 
    example'.split()
    ['This', 'is', 'a', 'example']
    
    >>> 'This 
    is a 
    example'.split(' ', 1)
    ['This', '
    is a 
    example'
    
    >>> 'This 
    is a 
    example'.splitlines()
    ['This ', 'is a ', 'example']
    
    >>> 'This 
    is a 
    example'.splitlines(True)
    ['This 
    ', 'is a 
    ', 'example']
    
    >>> 'www.example.com'.partition('.')
    ('www', '.', 'example.com')
    
    >>> 'www.example.com'.rpartition('.')
    ('www.example', '.', 'com')
    View Code

    7、填充ljust、center、rjust

    描述:

    ljust:返回一个指定的宽度 width 居左的字符串,fillchar 为填充的字符,默认为空格。如果指定的长度小于原字符串的长度则返回原字符串。

    center:返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。如果指定的长度小于原字符串的长度则返回原字符串。

    rjust:返回一个指定的宽度 width 居右的字符串,fillchar 为填充的字符,默认为空格。如果指定的长度小于原字符串的长度则返回原字符串。

    语法:

    str.ljust(width[, fillchar])
    str.center(width[, fillchar])
    str.rjust(width[, fillchar])
    • width -- 字符串的总宽度。
    • fillchar -- 填充字符。

    例如:

    >>> '[www.example.com]'.ljust(30, '*')
    '[www.example.com]*************'
    
    >>> '[www.example.com]'.rjust(30, '*')
    '*************[www.example.com]'
    
    >>> '[www.example.com]'.center(30, '*')
    '******[www.example.com]*******'
    
    >>> '[www.example.com]'.center(4, '*')
    '[www.example.com]'
    View Code

     8、join

    描述:

    将序列中的元素以指定的字符连接生成一个新的字符串。

    语法:

    str.join(sequence)
    • sequence -- 要连接的元素序列。

    例如:

    >>> '-'.join(('a', 'b', 'c'))
    'a-b-c'
    View Code

     9、isalpha、isdigit、isalnum、isspace

    描述:

    isalpha:检测字符串是否只由字母组成。

    isdigit:检测字符串是否只由数字组成。

    isalnum:检测字符串是否由字母和数字组成。

    isspace:检测字符串是否只由空格组成。

    语法:

    str.isalpha()
    str.isdigit()
    str.isalnum()
    str.isspace()

    例如:

    >>> 'abc'.isalpha()
    True
    
    >>> '123'.isdigit()
    True
    
    >>> 'abc123'.isalnum()
    True
    
    >>> '   '.isspace()
    True
    View Code
  • 相关阅读:
    基于socket的TCP和UDP编程
    (转)基于socket的TCP和UDP编程
    MFC的本质
    Windows程序内部运行机制 转自http://www.cnblogs.com/zhili/p/WinMain.html
    mysql主从切换
    mysql主从不一致解决方法
    postgresql+slony-i安装配置主从
    inndb存储引擎调优
    mysql 锁优化
    NDB Cluster 存储引擎物理备份
  • 原文地址:https://www.cnblogs.com/Jimc/p/9491590.html
Copyright © 2011-2022 走看看