Python03_字符串

字符串

  • 概念

    • 字符的集合:使用单、双、三引号引住的字符序列
    • 不可变对象
    • Unicode 类型(python3 + )
  • 字符串定义

    # 字符串定义
    >>> s1 = 'hello world 1'
    >>> s2 = "hello world 2"
    >>> s3 = '''this's a "string"'''	# 三引号中可随意使用单双引号
    >>> print(s1, s2, s3)
    hello world 1 hello world 2 this's a "string"
    
    # 转义字符\使用
    >>> s4 = 'c:\windows\nt'	# 若字符串不做特殊处理,该字符串中的\n会当作换行符使用
    >>>  print(s4)
    c:\windows
    t
    
    >>> s5 = r'c:\windows\nt'	# r前缀表示该字符串不做特殊处理
    >>> print(s5)
    c:\windows\nt
      
    >>> s6 = 'c:\windows\\nt'	# \\表示转义转义字符,将转义字符\当作普通字符使用
    >>> print(s6)
    c:\windows\nt
      
    >>> name = 'summer';age = 18
    >>> s7 = f'{name}, {age}'	# f前缀,3.6版本新增,格式化字符串
    >>> print(s7)
    summer, 18
    
    >>> s8 = '''select * from user where name='summer' '''	# 需注意,当三引号中开头或结尾需要使用单引号时,与三引号之间用空格间隔,或使用\转义,避免混淆导致报错
    >>> print(s8)
    select * from user where name='summer' 
    
  • 字符串操作

    • 字符串元素访问(下标)

      >>> s8 = '''select * from user where name='summer' '''
      >>> s8[4]		# 通过下标(索引)查看字符串
      'c'
      >>> s8[7] = '#'		# 字符串是不可变对象,不支持修改
      ---------------------------------------------------------------------------
      TypeError                                 Traceback (most recent call last)
      <ipython-input-15-a70f58756a35> in <module>
      ----> 1 s8[7] = '#'
      
      TypeError: 'str' object does not support item assignment
        
      >>> s7
      'summer, 18'
      >>> for i in s7: print(i, type(i))	# 字符串是有序的字符集合,字符序列,可迭代;type()查看类型
      s <class 'str'>
      u <class 'str'>
      m <class 'str'>
      m <class 'str'>
      e <class 'str'>
      r <class 'str'>
      , <class 'str'>
        <class 'str'>
      1 <class 'str'>
      8 <class 'str'>
      
      >>> list(s7)	# 原字符串不变,生成新的列表并将字符串里的元素迭代写入列表中
      ['s', 'u', 'm', 'm', 'e', 'r', ',', ' ', '1', '8']
      
    • 字符串连接 join()、+

      • join格式"string".join(iterable)
        • 将可迭代对象连接起来,使用string作为分隔符,string本身也为字符串
        • 可迭代对象本身元素必须是字符串
        • 返回一个新的字符串
      >>> name = 'summer'
      >>> print(".".join(name))	# 使用字符.将各个字符连接起来,并返回一个新的字符串
      s.u.m.m.e.r
      >>> print("\t".join(name))	# 使用制表符\t连接各个元素
      s	u	m	m	e	r
      >>> print("\n".join(name))	# 使用换行符\n连接
      s
      u
      m
      m
      e
      r
      
      >>> lst = ['1', ['a', 'b'], '3']
      >>> '+'.join(lst)		# 可迭代的对象必须是字符串,该对象为列表,所以导致报错
      ---------------------------------------------------------------------------
      TypeError                                 Traceback (most recent call last)
      <ipython-input-65-a3ae9ea2c07f> in <module>
      ----> 1 '+'.join(lst)
      
      TypeError: sequence item 1: expected str instance, list found
          
      
      >>> '+'.join(map(str, lst))		# 使用map()函数,将列表转换成字符串后,可连接成功
      "1+['a', 'b']+3"
      
      >>> "*".join(map(str,range(9)))	# 同理,需使用map()函数转换成字符串后进行连接
      '0*1*2*3*4*5*6*7*8'
      
      • +连接
        • 将两个字符串连接起来并返回一个新字符串
      >>> name = 'summer '
      >>> age = 'is very good'
      >>> name + age	# 连接两个字符串并返回一个新的字符串
      'summer is very good'
      
    • 字符串分割

      • split(sep=None, maxsplit=-1)

        • 从左至右,sep指定分割字符串,缺省值为空白字符串(一个或多个)

        • maxsplit指定分割的次数,-1表示遍历整个字符串

          >>> web = 'http:  www.baidu.com index.html'
          >>> web.split()		# 默认使用一个或多个空白字符串,分割成3段
          ['http:', 'www.baidu.com', 'index.html']
          
          >>> web.split(' ')	# 指定使用1个空白字符分割,分割成4段
          ['http:', '', 'www.baidu.com', 'index.html']
          
          >>> web.split('.')		# 使用.做分隔符
          ['http:  www', 'baidu', 'com index', 'html']
          
          >>> web.split('.', maxsplit=1)	# 使用.做分隔符,且最多分割1次,分割成2段
          ['http:  www', 'baidu.com index.html']
          
          
          
      • split(sep=None, maxsplit=-1)

        • 从右至左分割,输出字符串顺序不变

        • sep指定分割字符串,缺省值为空白字符串(一个及多个)

        • maxsplit指定分割次数,-1表示遍历整个字符串

          >>> web
          'http:  www.baidu.com index.html'
          
          >>> web.rsplit()	# 从右至左分割后,字符串顺序不变
          ['http:', 'www.baidu.com', 'index.html']
          
          >>> web.rsplit('.')
          ['http:  www', 'baidu', 'com index', 'html']
          
          >>> web.rsplit('baidu')
          ['http:  www.', '.com index.html']
          
          >>> web.rsplit('.', maxsplit=1)	# 从右至左,以字符.进行分割1次
          ['http:  www.baidu.com index', 'html']
          
          >>> web.rsplit('.', maxsplit=2)	# 从右至左,以字符.进行分割2次
          ['http:  www.baidu', 'com index', 'html']
          
          
      • splitlines([keepends])

        • 按照行来切分字符串

        • keepends指是否保留行分隔符,行分隔符包括:\n(Linux) \r\n(Windows) \r(MacOs)等

          >>> s1 = "I'm\na\r\npandas\r\n!"
          >>> print(s1)
          I'm
          a
          pandas
          !
          >>> s1.splitlines()	# 默认以换行符分割
          ["I'm", 'a', 'pandas', '!']
          
          >>> s1.splitlines(True)	# 以换行符分割,切保留分隔符
          ["I'm\n", 'a\r\n', 'pandas\r\n', '!']
          
      • partition(sep)

        • 从左至右,遇到分隔符就把字符串分割成两部分,并返回头(head)、分隔符、尾部(tail)三部分的三元组,若未找到分隔符,则返回头、2个空元素的三元组

        • sep指定分割字符串,(必须指定)

          >>> s2 = "I'm a pandas !"
          >>> s2
          "I'm a pandas !"
          
          >>> s2.partition(' ')		# 以空格为分隔符
          ("I'm", ' ', 'a pandas !')
          
          >>> s2.partition('a')	# 从左至右,以字符a为分隔符,遇到第一个a进行分割,返回三元组
          ("I'm ", 'a', ' pandas !')
          
          >>> s2.rpartition('a')	# 从右至左,以字符a为分隔符,遇到第一个a进行分割,返回三元组
          ("I'm a pand", 'a', 's !')
          
    • 字符串大小写(可用于做判断)

      • upper()全大写

        >>> s2 = "I'm A pandas !"
        >>> s2.upper()
        "I'M A PANDAS !"
        
      • lower()全小写

        >>> s2.lower()
        "i'm a pandas !"
        
      • swapcase()大小写交换

        >>> s2.swapcase()
        "i'M a PANDAS !"
        
    • 字符串排版

      • title()标题的每个单词首字符都大写

        >>> s3 = "be ready to take off !"
        >>> s3
        'be ready to take off !'
        >>> s3.title()
        'Be Ready To Take Off !'
        
      • capitalize()首个单词大写

        >>> s3.capitalize()
        'Be ready to take off !'
        
        
      • center(width[, fillchar])居中

        • width打印宽度

        • fillchar填充的字符

          >>> s3.center(50)	# 打印宽度为50并居中
          '              be ready to take off !              '
          
          >>> s3.center(50,'-')	# 打印宽度为50并居中,空白字符用-填充
          '--------------be ready to take off !--------------'
          
          
      • zfill(width)

        • width打印宽度,居右,左边用0填充

          >>> s3.zfill(50)
          '0000000000000000000000000000be ready to take off !'
          
          
      • ljust(width[, fillchar])左对齐

        >>> s3.ljust(50)
        'be ready to take off !                            '
        
        >>> s3.ljust(50, '+')
        'be ready to take off !++++++++++++++++++++++++++++'
        
      • rjust(width[, fillchar])右对齐

        >>> s3.rjust(50)
        '                            be ready to take off !'
        
        >>> s3.rjust(50, '=')
        '============================be ready to take off !'
        
    • 字符串修改

      • replace(old, new[, count])

        • 从字符串中找到匹配字符串,并替换为新字符串,返回新字符串

        • count表示替换几次,不指定则全部替换

          >>> web = 'http://www.baidu.com/index.html'
          
          >>>web.replace('.', '_')	# 将所有的.替换成_
          'http://www_baidu_com/index_html'
          
          >>> web.replace('.', '^', 2)	# 将前两个.替换成^
          'http://www^baidu^com/index.html'
          
          >>> web.replace('w', 'Q', 1)	# 将第一个w替换成Q
          'http://Qww.baidu.com/index.html'
          
      • strip([chars])

        • 从字符串两端去除指定的字符集chars中的所有字符

        • 如果chars没有指定,去除两端的空白字符

        • lstrip([chars])从左开始去除rstrip([char])从右开始去除

          >>> s6 = "\t be ready to take off, ok \n"
          >>> s6
          '\t be ready to take off, ok \n'
          
          >>> s7 = s6.strip()		#默认去除两边的空白字符
          >>> s7
          'be ready to take off, ok'
          '
          >>> s7.strip('k').strip('o').strip('be')	# 依次去除两边的'k','o','be'
          ' ready to take off, '
          
          
          >>> s7 = 'be ready to take off, be'
          
          >>> s7.rstrip('be')	# 默认去除两端的'be'
          'be ready to take off, '
          
          >>> s7.lstrip('be')	# 从左开始去除'be'
          ' ready to take off, be'
          
          >>> s7.strip('be')	# 从右开始去除‘be'
          ' ready to take off, '
          
          
    • 字符串查找

      • find(sub[, start[, end]])时间复杂度O(n)

        • 在指定区间(start, end)从左至右查找字符串sub,找到返回正索引,没找到返回-1

          >>> s7
          'be ready to take off, be'
          >>> s7.find('a')	# 查找字符'a',找到并返回正索引
          5
          >>> s7.find('t',5, 10)  # 在索引5到10之间查找字符't',找到返回正索引
          9
          >>> s7.find('t',0, 5)		# 在索引0到5之间查找字符't',未找到,返回-1
          -1
          >>> s7.find('t',-20, -1)	# 在索引位置-1到-20之间,从左至右查找字符串't',找到返回正索引
          9
          
      • rfind(sub[, start[, end]])时间复杂度O(n)

        • 在指定区间(start, end)从右至左查找字符串sub,找到返回正索引,没找到返回-1

          >>> s7
          'be ready to take off, be'
          >>> s7.rfind('e')	# 从右至左查找字符串'e', 找到返回正索引
          23
          
          >>> s7.rfind('e', 0, 5)		# 在索引位置0到5之间,从右至左查找字符串't',找到返回正索引
          4
          
          
      • index(sub[, start[, end]])时间复杂度O(n)

        • 在指定区间(start, end)从左至右查找字符串sub,找到返回正索引,未找到抛出异常ValueError

          >>> s7
          'be ready to take off, be'
          >>> s7.index('a', 3, 12)		# 在索引位置3到12之间,从左至右查找字符串'a',找到返回正索引
          5
          
          >>> s7.index('zhao')		# 从左至右查找字符串'zhao',未找到抛出异常
          ---------------------------------------------------------------------------
          ValueError                                Traceback (most recent call last)
          <ipython-input-200-5431288decf2> in <module>
          ----> 1 s7.index('z')
          
          ValueError: substring not found
          
      • rindex(sub[, start[, end]])时间复杂度O(n)

        • 在指定区间(start, end)从右至左查找字符串sub,找到返回正索引,未找到抛出异常ValueError

          >>> s7
          'be ready to take off, be'
          >>> s7.rindex('e', -8)	# 在-8至最后的区间中从右至左查找字符'e',找到返回正索引
          23
          
          >>> s7.rindex('e', -15, -8)		# 在-1至-8的区间中从右至左查找字符'e',找到返回正索引
          15
          
          >>> s7.rindex('zz', -20, -2)		# 在-20到-2之间查找'zz',未找到,抛出异常
          ---------------------------------------------------------------------------
          ValueError                                Traceback (most recent call last)
          <ipython-input-212-77923f715329> in <module>
          ----> 1 s7.rindex('zz', -20, -2)
          
          ValueError: substring not found
          
      • count(sub[, start[, end]])时间复杂度O(n)

        • 在指定区间(start, end)从左至右,统计字符串sub出现的次数

          >>> s7
          'be ready to take off, be'
          
          >>> s7.count('ready')	 #查找字符串中字符串'ready'出现的次数
          1
          
          >>> s7.count('e')		#查找字符串中字符串'e'出现的次数
          4
          
          >>> s7.count('e', 1, 10)	#在区间1到10之间,查找字符串中字符串'e'出现的次数
          2
          
          >>> s7.count('kk')	# 查找字符串'kk'出现的次数
          0
          
      • len(string)返回字符串长度

        >>> s7
        'be ready to take off, be'
        >>> len(s7)	# 查看字符串的长度
        24
        
    • 字符串判断

      • endswith(suffix[, start[, end]])

        • 在指定区间(start, end),判断是否是suffix结尾

          >>> s7
          'be ready to take off, be'
          
          >>> s7.endswith('be')	# 判断字符串是否以'be'结尾
          True
          
          >>> s7.endswith('be', 1, 10)	# 在索引区间1到10之间,判断是否以'be'结尾
          False
          
      • startswith(prefix,[start[, end]])

        • 在指定区间(start, end),判断是否是prefix开头

          >>> web
          'http://www.baidu.com/index.html'
          
          >>> web.startswith('ttp')		# 判断字符串是否以'ttp'开头
          False
          
          >>> web.startswith('www', 7, 15)	# 在索引位置7到15之间,判断是否以'www'开头
          True
          
          
      • is系列

        • isalnum()是否是字母和数字组成
        • isalpha()是否是字母
        • isdecimal()是否只包含十进制数字
        • isdigit()是否为全部数字(0-9)
        • isidentifier 是不是字母和下划线开头,其他都是字母、数字、下划线(可用于判断标识符)
        • islower()是否都是小写
        • isupper()是否都是大写
        • isspace()是否只包含空白字符
    • 字符串格式化printf-style formatting

      • `来自C语言的printf函数

      • 格式要求:

        • 占位符:使用%和格式字符组成,例如:%s(调用str()), %d整数,%r(调用repr())、%f浮点数%x十六进制整数

          >>> 'Age: %d, %s Cool' % (18, 'Very')
          'Age: 18, Very Cool'
          
          >>> 'pi = %f' % 3.1415926
          'pi = 3.141593'
          
          >>> '%x' % 127
          '7f'
          
    • 字符串格式化format(): Python鼓励使用

      • 格式:"{} {xxx}".format(*args, **kwargs)

      • args是可变位置参数

      • kwargs是可变关键字参数,是一个字典

      • {}花括号表示占位符,按照顺序匹配位置参数,{n}表示取位置参数索引为n的值

      • {xxx}表示关键字参数中,搜索名称一致的

      • {{}}表示打印花括号

        >>> print("my name is {}, age{}".format('summer','18'))
        my name is summer, age18
        
        >>> print("{} / {} = {:f}".format(10, 3, 10 / 3))		# 默认精度为6
        10 / 3 = 3.333333
        
        >>> print("{} / {} = {:.2}".format(10, 3, 10 / 3))	# 宽度为2个数字
        10 / 3 = 3.3
        
        >>> print("{} / {} = {:.2f}".format(10, 3, 10 / 3))		# 小数点后两位
        10 / 3 = 3.33
        
        >>> print("{:>20}".format(11.11))		# 宽度20,右对齐
                       11.11
          
        >>> print("{:^20}".format(11.11))		# 宽度20,居中
               11.11        
        
        >>> print("{:20.2f}".format(3 ** 0.5))	# 宽度20,精度小数点后2位
                        1.73
        
        >>> print("{:3.3%}".format(3 ** 0.5))	 # 宽度3,求百分比
        173.205%
        
        # 注:宽度可被撑破
        
posted @ 2019-08-01 20:58  Summer_DHY  阅读(137)  评论(0)    收藏  举报