符号串常量
单双引号字符串是一样的
- 字符串以’’或””形式进行书写;两种形式同样有效并返回相同类型的对象
>>> a = 'jam'
>>> b = "jam"
>>> a
'jam'
>>> b
'jam'
>>>
- 两个字符串中间加上’,’会以元组的形式呈现
>>> c = 'jam', 'jar'
>>> c
('jam', 'jar')
- 在不使用转义字符的情况下,依然可以实现句中包含单引号 ‘ 或多引号 “的情况
>>> a = "i'm a boy"
>>> a
"i'm a boy"
>>> b = "i'm" 'a boy'
>>> b
"i'ma boy"
用转义序列代表特殊字节
- 当引号使用冲突时,除可以使用单双引号交替写入外,还可以在引号前加入反斜杠的形式。即转义序列,反斜杠引入特殊的字节编码(如不容易通过键盘输入的字节)。
反斜杠使用方式
>>> a = 'i\'m a boy'
>>> a
"i'm a boy"
反斜杠输入键盘不易输入的字节(换行符:\n;制表符:\t )
>>> a = 'I\'m a \n boy'
>>> a
"I'm a \n boy"
>>> print(a)
I'm a
boy
- 含有转义序列字符串的字节查看方式:反斜杠存在的意义是告诉python其后为特殊字符需要保存,而反斜杠本身并不进行存储。使用len()函数查看。
>>> a = 'I\'m \na boy and \tyou'
>>> a
"I'm \na boy and \tyou"
>>> print(a)
I'm
a boy and you
>>> len(a)
19
>>> a = 'I\'m a boy and you'
>>> len(a)
17
- 转义序列与二进制和十六进制(不写)
- 按照规定识别与识别不出区别。超出特别识别符范围的输入。(python中特殊字节能够识别,如果超出范围,则不可以识别)
>>> a = 'I\'m a boy and \o a girl '
>>> print(a)
I'm a boy and \o a girl
raw字符串抑制转义
raw字符抑制转义主要是在写文件路径,如:’D:\500 practice\504 Ceshi’时,会包含反斜杠。
>>> a='D:\500 practice\504 Ceshi'
>>> a
'D:ŀ practiceń Ceshi'
>>> print(a)
D:ŀ practiceń Ceshi
- 缘合使用raw字符(两种方式)
>>> a = r'D:\500 practice\504 Ceshi'
>>> a
'D:\\500 practice\\504 Ceshi'
>>> print(a)
D:\500 practice\504 Ceshi
- 或者使用反斜杠去抑制转义
>>> a = 'D:\\500 practice\\504 Ceshi'
>>> a
'D:\\500 practice\\504 Ceshi'
>>> print(a)
D:\500 practice\504 Ceshi
三重引号编写多行字符串块
>>> """ I'm a good boy
从今天起 喂马
劈柴 周游世界"""
" I'm a good boy\n从今天起 喂马\n劈柴 周游世界"
>>> print(""" I'm a good boy
从今天起 喂马
劈柴 周游世界""")
I'm a good boy
从今天起 喂马
劈柴 周游世界
实际应用中的字符串
基本操作
- 字符串的组合(+ *),注意字符串的组合的相关方法。
字符串+字符串 字符串+数字
>>> 'abc' + 'def'
'abcdef'
>>> 'abc'+0
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
'abc'+0
TypeError: can only concatenate str (not "int") to str
- for语句 、in 表达式与 string.find()函数区别
for语句与in表达式连用,用于打印字符串或列表中的对象;
in表达式用于检测字符串或列表中是否包含某一对象,如果在则返回True,不在则返回False;
string.find()函数也是检测某一项是否在某一对象中,如果在则返回具体的偏移值,不在则返回-1
>>> str = 'abcdefgh'
>>> 'ab'in str
True
>>> 'ik' in str
False
>>> str.find('ab')
0
>>> str.find('ik')
-1
>>> for i in str:
print(i, end = '')
abcdefgh
索引和分片
- 偏移量(正偏移量和负偏移量)索引
>>> a = 'abcdefg'
>>> a[0]
'a'
- 切片
>>> a[0:2]
'ab'
- 步移
>>> a[0:7:2]
'aceg'
字符串转换工具
- 字符串与asci的转换
字符串->ASCⅡ ord()
ASCⅡ->字符串 chr()
>>> a = '1'
>>> ord(a)
49
>>> chr(49)
'1'
- 字符串与数字转换
字符串->数字:整型 int();浮点型 float()
数字 ->字符串: str()
字符串是不可原位修改的,所以在改变类型后,需要重新给变量a赋值。
>>> a = '1'
>>> type(a)
<class 'str'>
>>> a = int(a)
>>> a
1
>>> type(a)
<class 'int'>
字符串方法
字符串方法实例:修改字符串
- 利用切片和合并
>>> str = 'abcdddefgggh'
>>> str = str[:3] + 'aaa' + str[6:]
>>> str
'abcaaaefgggh'
- 利用replace方法
>>> str = str.replace('a', 'd')
>>> str
'dbcdddefgggh'
- 将字符串转化成列表
字符串是序列中无法更改的对象,可以将其改为可更改的列表,使用列表的替换工具进行操作,而后使用.join()函数将列表转换为str。
>>> lis = list(str)
>>> lis
['d', 'b', 'c', 'd', 'd', 'd', 'e', 'f', 'g', 'g', 'g', 'h']
>>> lis[0]='a'
>>> lis
['a', 'b', 'c', 'd', 'd', 'd', 'e', 'f', 'g', 'g', 'g', 'h']
>>> str = ''.join(lis)
>>> str
'abcdddefgggh'
join()使用说明
.join前 为间隔符,可以插入想区分的符号或字符串或其他
>>> str = ','.join(lis)
>>> str
'a,b,c,d,d,d,e,f,g,g,g,h'
>>> str = '魏'.join(lis)
>>> str
'a魏b魏c魏d魏d魏d魏e魏f魏g魏g魏g魏h'
字符串方法实例:文本解析
有固定偏移:切片
有间隔符:split()
>>> str = 'abcdefgh'
>>> str[0:2]
'ab'
>>> str = 'abc def ghi'
>>> str.split(' ')
['abc', 'def', 'ghi']
实际应用中的其他常见字符串方法
字符串格式化表达式
更高级的字符串格式化表达式\
基于字典的字符串格式化
字符串格式化调用方法
基础知识
- format()函数基础知识:通过位置和关键字名称进行传递
>>> str = '{},{} and {}'
>>> str.format('a', 'b', 'c')
'a,b and c'
>>> str = '{0}, {1} and {2}'
>>> str.format('a', 'b', 'c')
'a, b and c'
>>> str.format(name = 'a', age = 'b', year = 'c')
'a, b and c'
添加键、属性和偏移量
通过列表索引
lis = [2, 3, 5]
>>> str = '{0[0]}, {0[1]}'
>>> str.format(lis)
'2, 3'
>>> str = '{0}, {1}'
>>> str.format(lis[0], lis[1])
'2, 3'
添加具体格式化
字符宽度;对齐方式;小数点个数;数字类型
宽度和对齐方式
>>> '{0:^8}'.format(2) # 8个字节居中
' 2 '
>>> '{0:>8}'.format(2) # 8个字节右对齐
' 2'
>>> '{0:<8}'.format(2) # 8个字节左对齐
'2 '
小数点个数和数字类型
>>> '{0:^8.3f}'.format(2.3) # 8字节居中对齐保留3位小数
' 2.300 '