第7章 字符串 - 图1 第7章 字符串 - 图2

符号串常量

单双引号字符串是一样的

  1. 字符串以’’或””形式进行书写;两种形式同样有效并返回相同类型的对象
  1. >>> a = 'jam'
  2. >>> b = "jam"
  3. >>> a
  4. 'jam'
  5. >>> b
  6. 'jam'
  7. >>>
  1. 两个字符串中间加上’,’会以元组的形式呈现
  1. >>> c = 'jam', 'jar'
  2. >>> c
  3. ('jam', 'jar')
  1. 在不使用转义字符的情况下,依然可以实现句中包含单引号 ‘ 或多引号 “的情况
  1. >>> a = "i'm a boy"
  2. >>> a
  3. "i'm a boy"
  4. >>> b = "i'm" 'a boy'
  5. >>> b
  6. "i'ma boy"

用转义序列代表特殊字节

  1. 当引号使用冲突时,除可以使用单双引号交替写入外,还可以在引号前加入反斜杠的形式。即转义序列,反斜杠引入特殊的字节编码(如不容易通过键盘输入的字节)。

反斜杠使用方式

  1. >>> a = 'i\'m a boy'
  2. >>> a
  3. "i'm a boy"

反斜杠输入键盘不易输入的字节(换行符:\n;制表符:\t )

  1. >>> a = 'I\'m a \n boy'
  2. >>> a
  3. "I'm a \n boy"
  4. >>> print(a)
  5. I'm a
  6. boy
  1. 含有转义序列字符串的字节查看方式:反斜杠存在的意义是告诉python其后为特殊字符需要保存,而反斜杠本身并不进行存储。使用len()函数查看。
  1. >>> a = 'I\'m \na boy and \tyou'
  2. >>> a
  3. "I'm \na boy and \tyou"
  4. >>> print(a)
  5. I'm
  6. a boy and you
  7. >>> len(a)
  8. 19
  9. >>> a = 'I\'m a boy and you'
  10. >>> len(a)
  11. 17
  1. 转义序列与二进制和十六进制(不写)
  2. 按照规定识别与识别不出区别。超出特别识别符范围的输入。(python中特殊字节能够识别,如果超出范围,则不可以识别)
  1. >>> a = 'I\'m a boy and \o a girl '
  2. >>> print(a)
  3. I'm a boy and \o a girl

raw字符串抑制转义

raw字符抑制转义主要是在写文件路径,如:’D:\500 practice\504 Ceshi’时,会包含反斜杠。

  1. >>> a='D:\500 practice\504 Ceshi'
  2. >>> a
  3. 'D:ŀ practiceń Ceshi'
  4. >>> print(a)
  5. D practiceń Ceshi
  1. 缘合使用raw字符(两种方式)
  1. >>> a = r'D:\500 practice\504 Ceshi'
  2. >>> a
  3. 'D:\\500 practice\\504 Ceshi'
  4. >>> print(a)
  5. D:\500 practice\504 Ceshi
  1. 或者使用反斜杠去抑制转义
  1. >>> a = 'D:\\500 practice\\504 Ceshi'
  2. >>> a
  3. 'D:\\500 practice\\504 Ceshi'
  4. >>> print(a)
  5. D:\500 practice\504 Ceshi

三重引号编写多行字符串块

  1. >>> """ I'm a good boy
  2. 从今天起 喂马
  3. 劈柴 周游世界"""
  4. " I'm a good boy\n从今天起 喂马\n劈柴 周游世界"
  5. >>> print(""" I'm a good boy
  6. 从今天起 喂马
  7. 劈柴 周游世界""")
  8. I'm a good boy
  9. 从今天起 喂马
  10. 劈柴 周游世界

实际应用中的字符串

基本操作

  1. 字符串的组合(+ *),注意字符串的组合的相关方法。

字符串+字符串 字符串+数字

  1. >>> 'abc' + 'def'
  2. 'abcdef'
  3. >>> 'abc'+0
  4. Traceback (most recent call last):
  5. File "<pyshell#24>", line 1, in <module>
  6. 'abc'+0
  7. TypeError: can only concatenate str (not "int") to str
  1. for语句 、in 表达式与 string.find()函数区别

for语句与in表达式连用,用于打印字符串或列表中的对象;
in表达式用于检测字符串或列表中是否包含某一对象,如果在则返回True,不在则返回False;
string.find()函数也是检测某一项是否在某一对象中,如果在则返回具体的偏移值,不在则返回-1

  1. >>> str = 'abcdefgh'
  2. >>> 'ab'in str
  3. True
  4. >>> 'ik' in str
  5. False
  6. >>> str.find('ab')
  7. 0
  8. >>> str.find('ik')
  9. -1
  10. >>> for i in str:
  11. print(i, end = '')
  12. abcdefgh

索引和分片

  1. 偏移量(正偏移量和负偏移量)索引
  1. >>> a = 'abcdefg'
  2. >>> a[0]
  3. 'a'

image.png

  1. 切片
  1. >>> a[0:2]
  2. 'ab'
  1. 步移
  1. >>> a[0:7:2]
  2. 'aceg'

字符串转换工具

  1. 字符串与asci的转换

字符串->ASCⅡ ord()
ASCⅡ->字符串 chr()

  1. >>> a = '1'
  2. >>> ord(a)
  3. 49
  4. >>> chr(49)
  5. '1'
  1. 字符串与数字转换

字符串->数字:整型 int();浮点型 float()
数字 ->字符串: str()
字符串是不可原位修改的,所以在改变类型后,需要重新给变量a赋值。

  1. >>> a = '1'
  2. >>> type(a)
  3. <class 'str'>
  4. >>> a = int(a)
  5. >>> a
  6. 1
  7. >>> type(a)
  8. <class 'int'>

字符串方法

字符串方法实例:修改字符串

  1. 利用切片和合并
  1. >>> str = 'abcdddefgggh'
  2. >>> str = str[:3] + 'aaa' + str[6:]
  3. >>> str
  4. 'abcaaaefgggh'
  1. 利用replace方法
  1. >>> str = str.replace('a', 'd')
  2. >>> str
  3. 'dbcdddefgggh'
  1. 将字符串转化成列表

字符串是序列中无法更改的对象,可以将其改为可更改的列表,使用列表的替换工具进行操作,而后使用.join()函数将列表转换为str。

  1. >>> lis = list(str)
  2. >>> lis
  3. ['d', 'b', 'c', 'd', 'd', 'd', 'e', 'f', 'g', 'g', 'g', 'h']
  4. >>> lis[0]='a'
  5. >>> lis
  6. ['a', 'b', 'c', 'd', 'd', 'd', 'e', 'f', 'g', 'g', 'g', 'h']
  7. >>> str = ''.join(lis)
  8. >>> str
  9. 'abcdddefgggh'

join()使用说明
.join前 为间隔符,可以插入想区分的符号或字符串或其他

  1. >>> str = ','.join(lis)
  2. >>> str
  3. 'a,b,c,d,d,d,e,f,g,g,g,h'
  4. >>> str = '魏'.join(lis)
  5. >>> str
  6. 'a魏b魏c魏d魏d魏d魏e魏f魏g魏g魏g魏h'

字符串方法实例:文本解析

  1. 有固定偏移:切片

  2. 有间隔符:split()

  1. >>> str = 'abcdefgh'
  2. >>> str[0:2]
  3. 'ab'
  4. >>> str = 'abc def ghi'
  5. >>> str.split(' ')
  6. ['abc', 'def', 'ghi']

实际应用中的其他常见字符串方法

()

字符串格式化表达式

更高级的字符串格式化表达式\

基于字典的字符串格式化

字符串格式化调用方法

基础知识

  1. format()函数基础知识:通过位置和关键字名称进行传递
  1. >>> str = '{},{} and {}'
  2. >>> str.format('a', 'b', 'c')
  3. 'a,b and c'
  4. >>> str = '{0}, {1} and {2}'
  5. >>> str.format('a', 'b', 'c')
  6. 'a, b and c'
  7. >>> str.format(name = 'a', age = 'b', year = 'c')
  8. 'a, b and c'

添加键、属性和偏移量

通过列表索引

  1. lis = [2, 3, 5]
  2. >>> str = '{0[0]}, {0[1]}'
  3. >>> str.format(lis)
  4. '2, 3'
  5. >>> str = '{0}, {1}'
  6. >>> str.format(lis[0], lis[1])
  7. '2, 3'

添加具体格式化

字符宽度;对齐方式;小数点个数;数字类型

  1. 宽度和对齐方式

    1. >>> '{0:^8}'.format(2) # 8个字节居中
    2. ' 2 '
    3. >>> '{0:>8}'.format(2) # 8个字节右对齐
    4. ' 2'
    5. >>> '{0:<8}'.format(2) # 8个字节左对齐
    6. '2 '
  2. 小数点个数和数字类型

  1. >>> '{0:^8.3f}'.format(2.3) # 8字节居中对齐保留3位小数
  2. ' 2.300 '

与%格式化表达式比较

为什么用新的格式化方法

通常意义下的类型分类

同样分类的类型共享其操作集合

可变类型能够在原处修改