4.字符串String

作者: Stone_説 | 来源:发表于2020-12-12 22:42 被阅读0次

    目录
    0.字符串介绍
    1.字符串定义和初始化
    2.字符串元素访问
    3.字符串连接
    4.字符串分割
    5.字符串修改
    6.字符串判断
    7.字符串格式化

    0.字符串介绍

    一个个字符组成的有序的序列,是字符的集合
    使用单引号、双引号、三引号引住的字符序列
    字符串是不可变对象
    从Python3开始,字符串就是Unicode类型

    1.字符串定义和初始化

    r和R前缀会使字符串中的转义字符无效

    >>> s1 = 'string'
    >>> s2 = "string"
    >>> print(s1,s2)
    string string
    >>> s3 = '''this is a "String" '''
    >>> print(s3)
    this is a "String" 
    >>> s4 = 'hello \n magedu.com'
    >>> print(s4)
    hello 
     magedu.com
    >>> s5 = r"hello \n magedu.com"
    >>> print(s5)
    hello \n magedu.com
    >>> s7 = R"c:\windows\nt"
    >>> print(s7)
    c:\windows\nt
    >>> s8 = 'c:\windows\\nt'
    >>> print(s8)
    c:\windows\nt
    >>> sql = """select * from user where name='tom'"""
    >>> sql
    "select * from user where name='tom'"
    >>> print(sql)
    select * from user where name='tom'
    

    2.字符串元素访问

    2.1 下标访问

    下标访问

    >>> str1 = "abcdefg"
    >>> str1[1]
    'b'
    

    循环迭代

    >>> for c in str1:
    ...     print(c,type(c))
    ... 
    a <class 'str'>
    ...
    g <class 'str'>
    >>> lst = list(str1)
    >>> lst
    ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    
    2.2 字符串查找 --> find方法

    find(sub[,start[,end]]) -> int
    在指定的区间[start,stop),从左至右,查找子串sub。
    找到返回索引,没找到返回-1

    >>> s1 = "I am stone stone stone stone life"
    >>> s1.find("stone",5)
    5
    >>> s1.find("stone",6,13)
    -1
    >>> s1.find("stone",10)
    11
    >>> s1.find("stone",10,15)
    -1
    >>> s1.find("stone",-20,-1)
    17
    

    rfind(sub[,start[,end]]) -> int
    在指定的区间[start,stop),从右至左,查找子串sub。
    找到返回索引,没找到返回-1

    >>> s1 = "I am stone stone stone stone life"
    >>> s1.rfind("stone",-20,-1)
    23
    >>> s1.rfind("stone",10,15)
    -1
    >>> s1.rfind("stone",10)
    23
    
    2.3 字符串查找 --> index方法

    index(sub[,start[,end]]) -> int
    在指定的区间[start,stop),从左至右,查找子串sub。
    找到返回索引,没找到返回ValueError

    >>> s1 = "I am stone stone stone stone life"
    >>> s1.index('stone')
    5
    >>> s1.index('stone',6,13)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: substring not found
    >>> s1.index('stone',6,20)
    11
    

    rindex(sub[,start[,end]]) -> int
    在指定的区间[start,stop),从右至左,查找子串sub。
    找到返回索引,没找到返回ValueError

    >>> s1 = "I am stone stone stone stone life"
    >>> s1.rindex('stone',-10,-1)
    23
    >>> s1.rindex('stone',-2,-1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: substring not found
    
    2.4 返回字串个数 --> count方法

    count(sub[,start[,end]]) -> int
    在指定的区间[start,end),从左至右,统计字串sub出现的次数

    >>> s1 = "I am stone stone stone stone life"
    >>> s1
    'I am stone stone stone stone life'
    >>> s1.count("stone")
    4
    >>> s1.count("a")
    1
    
    2.5 返回字符串长度 --> len(str)
    >>> s1 = "I am stone stone stone stone life"
    >>> len(s1)
    33
    

    3.字符串连接

    3.1 jion连接 -> "string".join(iterable) -> str

    将可迭代对象连接起来,使用string作为分隔符
    可迭代对象本身元素必须是字符串
    返回一个新字符串

    >>> lst1 = [1,2,3]    #列表中元素不是字符串类型,无法拼接
    >>> "**".join(lst1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: sequence item 0: expected str instance, int found
    >>> lst2 = ['1','2','3']
    >>> "**".join(lst2)
    '1**2**3'
    >>> "  ".join(lst2)
    '1  2  3'
    >>> "\n".join(lst2)
    '1\n2\n3'
    >>> print("\n".join(lst2))
    1
    2
    3
    >>> print("\\n".join(lst2))
    1\n2\n3
    >>> lst3 = ['1',['a','b'],'3']
    >>> "**".join(lst3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: sequence item 1: expected str instance, list found
    
    3.2 “+”号连接

    将2个字符串连接在一起
    返回一个新字符串

    >>> s1 = 'abc'
    >>> s2 = '1234'
    >>> s1 + s2
    'abc1234
    

    4.字符串分割

    4.1 split系 -> 将字符串按照分隔符分割成若干字符串,并返回列表

    split(sep=None,maxsplit=-1) -> list of ranges
    从左至右
    sep指定分割字符串,缺省的情况下空白字符串作为分隔符
    maxsplit指定分割的次数,-1表示遍历整个字符串

    >>> s1 = "I'm \ta good stone"
    >>> s1
    "I'm \ta good stone"
    >>> s1.split()
    ["I'm", 'a', 'good', 'stone']
    >>> s1.split('s')
    ["I'm \ta good ", 'tone']
    >>> s1.split('good')
    ["I'm \ta ", ' stone']
    >>> s1.split(' ',maxsplit=-1)
    ["I'm", '\ta', 'good', 'stone']
    >>> s1.split(' ',maxsplit=1)
    ["I'm", '\ta good stone']
    >>> s1.split(' ',maxsplit=2)
    ["I'm", '\ta', 'good stone']
    

    rsplit(sep=None,maxsplit=-1) -> list of ranges
    从右至左
    sep指定分割字符串,缺省的情况下空白字符串作为分隔符
    maxsplit指定分割的次数,-1表示遍历整个字符串

    >>> s1
    "I'm \ta good stone"
    >>> s1.rsplit()
    ["I'm", 'a', 'good', 'stone']
    >>> s1.rsplit('good')
    ["I'm \ta ", ' stone']
    >>> s1.split(' ',maxsplit=1)
    ["I'm", '\ta good stone']
    >>> s1.rsplit(' ',maxsplit=1)
    ["I'm \ta good", 'stone']
    

    splitlines([keepends]) -> list of strings
    按照行来切分字符串
    keepends指的是是否保留行分隔符
    行分隔符包括\n,\r\n,\r等

    >>> 'abc\n\nde fg\rkl\r\n'.splitlines()
    ['abc', '', 'de fg', 'kl']
    >>> 'abc\n\nde fg\rkl\r\n'.splitlines(True)
    ['abc\n', '\n', 'de fg\r', 'kl\r\n']
    >>> s1 = '''I'm a good stone
    ... You are . '''
    >>> s1
    "I'm a good stone\nYou are . "
    >>> print(s1.splitlines())
    ["I'm a good stone", 'You are . ']
    >>> print(s1.splitlines(True))
    ["I'm a good stone\n", 'You are . ']
    

    Note:第一个\n和第二个\n虽然没有字符,但是分隔的时候会产生一个空元素,同时\n结尾时,不会再有新的元素,但是如果不是连续两个\n一起时,则不会有空元素产生

    4.2 partition系 -> 将字符串按照分隔符分隔成2段,返回这2段和分隔符的元组

    partition(sep) -> (head,sep,tail)
    从左至右,遇到分隔符就把字符串分割成两部分,返回头,分隔符,尾三部分的三元组;
    如果没有找到分隔符,就返回头、2个空元素的三元组

    sep分割字符串,必须指定

    >>> s1 = "I'm \ta good stone"
    >>> s1
    "I'm \ta good stone"
    >>> s1.partition('s')
    ("I'm \ta good ", 's', 'tone')
    >>> s1.partition('')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: empty separator
    >>> s1.partition('abc')
    ("I'm \ta good stone", '', '')
    

    rpartition(sep) -> (head,sep,tail)
    从右至左,遇到分隔符就把字符串分割成两部分,返回头,分隔符,尾三部分的三元组;
    如果没有找到分隔符,就返回头、2个空元素的三元组

    >>> s1.rpartition('abc')
    ('', '', "I'm \ta good stone")
    >>> s1.rpartition('s')
    ("I'm \ta good ", 's', 'tone')
    >>> s1.rpartition()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: rpartition() takes exactly one argument (0 given)
    

    5.字符串修改

    5.1 replace(old,new[,count]) -> str

    字符串中找到匹配替换为新子串,返回新字符串
    count表示替换几次,不指定就是全部替换

    >>> 'www.stone.com'.replace('w','s')
    'sss.stone.com'
    >>> 'www.stone.com'.replace('w','s',1)
    'sww.stone.com'
    >>> 'www.stone.com'.replace('www','s',1)
    's.stone.com'
    
    5.2 strip([chars]) -> str

    从字符串两端去除指定的字符集chars中的所有字符
    如果chars没有指定,去除两端的空白字符

    >>> s1 = "\r \n \t Hello world\n \t"
    >>> s1
    '\r \n \t Hello world\n \t'
    >>> print(s1)
     
         Hello world
        
    >>> s1.strip()
    'Hello world'
    >>> s1
    '\r \n \t Hello world\n \t'
    >>> s2 = " I am very very very good "
    >>> s2
    ' I am very very very good '
    >>> s2.strip('Id')
    ' I am very very very good '
    >>> s2.strip('Id ')
    'am very very very goo'
    

    lstrip([chars]) -> str 从左开始
    rstrip([chars]) -> str 从右开始

    >>> s1 = "\r \n \t Hello world\n \t"
    >>> s1.lstrip()
    'Hello world\n \t'
    >>> s1.rstrip()
    '\r \n \t Hello world'
    

    6.字符串判断

    6.1 endswith && startswith

    endswith(suffix[,start[,end]]) -> bool
    在指定的区间[start,end),字符串是否是suffix结尾

    >>> s2 = 'I am very very very good'
    >>> s2.startswith('very')
    False
    >>> s2.startswith('very',5)
    True
    >>> s2.startswith('very',5,9)
    True
    >>> s2.startswith('very',5,10)
    True
    >>> s2.startswith('very',5,8)
    

    startswith(prefix[,start[,end]]) -> bool
    在指定的区间[start,end),字符串是否是prefix开头

    >>> s2 = 'I am very very very good'
    >>> s2.endswith('good')
    True
    >>> s2.endswith('ood')
    True
    >>> s2.endswith('good',5,100)
    True
    >>> s2.endswith('good',5,-1)
    False
    
    6.2 字符串判断is系列

    isalpha() 是否是字母
    isidentifier() 是不是字母和下划线开头,其他都是字母、数字、下划线
    isdecimal() 是否只包含十进制数字
    isdigit() 是否全部数字(0~9)
    isalnum() -> bool 是否是字母和数字组成
    islower() 是否都是小写
    isupper() 是否全部大写
    isspace() 是否只包含空白字符

    >>> "sdasdfsadf".isalpha()
    True
    >>> "abcd".isalpha()
    True
    >>> "abcd4234".isalpha()
    False
    >>> "abcd__4234".isidentifier()
    True
    >>> "abcd__423**4".isidentifier()
    False
    

    7.字符串格式化

    7.1 字符串大小写

    upper() -> 全部转换为大写字母
    lower() -> 全部转换为小写字母
    swapcase() -> 大小写交互

    >>> s1 = 'aBcDeF'
    >>> s1.upper()
    'ABCDEF'
    >>> s1.lower()
    'abcdef'
    >>> s1.swapcase()
    'AbCdEf
    
    7.2 字符串排版

    title() -> str 标题的每个单词都大写
    capitalize() -> str 首个单词大写
    center(width[,fillchar]) -> width:打印宽度 fillchar:填充的字符
    zfill(width) -> str width 打印宽度,居右,左边用0填充
    ljust(width[,fillchar]) -> str 左对齐
    rjust(width[,fillchar]) -> str 右对齐

    >>> s1 = "I am a stone"
    >>> s1.title()
    'I Am A Stone'
    >>> s1.capitalize()
    'I am a stone'
    >>> s1.center(20,'*')
    '****I am a stone****'
    >>> s1.center(4)
    'I am a stone'
    >>> s1.zfill(30)
    '000000000000000000I am a stone'
    >>> s1.ljust(30,'*')
    'I am a stone******************'
    >>> s1.rjust(30,'*')
    '******************I am a stone'
    
    7.3 format方法

    format函数格式字符串用法

    1. "{} {xxx}".format(*args,**kwargs) --> str 
    2. args是位置参数,是一个元组
    3. kwargs是关键字参数,是一个字典
    4. 花括号表示占位符
    5. {}表示按照顺序匹配位置参数,{n}表示取位置参数索引为n的值
    6. {xxx}表示在关键字参数中搜索名称一致的
    7. {{}}表示打印花括号
    

    位置参数
    按照顺序用位置参数替换前面的格式字符串的占位符中

    >>> "{}:{}".format('192.168.1.100',8888)
    '192.168.1.100:8888'
    

    关键字参数或命名参数
    位置参数按照序号匹配,关键字参数按照名词匹配

    >>> "{server} {1}:{0}".format(8888, '192.168.1.100', server='Web Server Info : ')
    'Web Server Info :  192.168.1.100:8888'
    

    元素访问

    >>> "{0[0]}.{0[1]}".format(('stone','com'))
    'stone.com'
    

    对象属性访问

    >>> from collections import namedtuple
    >>> Point = namedtuple('Point','x y')
    >>> p1 = Point(4,5)
    >>> "{{{0.x},{0.y}}}".format(p1)
    '{4,5}'
    >>> "{0.x}:{0.y}".format(p1)
    '4:5'
    

    字符串对齐

    >>> '{0}*{1}={2:<2}'.format(3,2,2*3)
    '3*2=6 '
    >>> '{0}*{1}={2:<02}'.format(3,2,2*3)
    '3*2=60'
    >>> '{0}*{1}={2:>02}'.format(3,2,2*3)
    '3*2=06'
    >>> '{:^30}'.format('centered')
    '           centered           '
    >>> '{:*^30}'.format('centered')
    '***********centered***********'
    

    进制

    >>> "int:{0:d};hex:{0:x};oct:{0:o};bin:{0:b}".format(42)
    'int:42;hex:2a;oct:52;bin:101010'
    >>> "int:{0:d};hex:{0:#x};oct:{0:#o};bin:{0:#b}".format(42)
    'int:42;hex:0x2a;oct:0o52;bin:0b101010'
    >>> octets = [192,168,0,1]
    >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)
    'C0A80001'
    

    相关文章

      网友评论

        本文标题:4.字符串String

        本文链接:https://www.haomeiwen.com/subject/dzpdgktx.html