一 、字符串
认识字符串首先认识字符串编码:
常用的字符串编码有: ASCII 、 GBK 、 UTF-8
编码与解码(爬取数据中会经常用到)
encode() 、 decode()
encode() 、 decode()括号中可根据编码类型进行编码和解码
# 编码: encode(): 将其他编码的字符串转换成Unicode编码
# 解码: decode(): 将Unicode编码转换成其他编码的字符串
a = '张三' print(type(a)) a1 = a.encode() # 编码 print(a1) print(type(a1)) # 编码后的类型 b = a1.decode() # 解码 print(b) print(type(b)) # 解码后的类型# bytes : 以字节为单位进行处理
字符串的常见操作
'''
字符串的常见操作:
find : 检测某个子串是否包含在某个字符串中,如果有,则返回子串开始位置,不存在就返回 -1
count : 返回某个字串在字符串出现的次数
replace : 替换 ('旧内容','新内容','替换次数')
split : 分割,指定分割符来切字符串
index : 跟find的作用一样,检测,当不存在时但会报异常
rfind = 和find()功能相同,但查找方向为右侧开始,没有存在就返回值为 -1
'''
str1 = 'hello world and java and c and python