- 编码:
8位=1byte 256
16位=2byte 65536
24位=3byte 16777216
GB2312–>GBK - 转化大小写
print(name1.upper()) # 转换为大写
print(name1.lower()) #转换为小写
- 索引字符串的位置
#索引为第一个字符串的位置 ,从第4个开始到第11 打印出来的是整型
print(name1.index("B根自从c进城",4,12))
- 编码
#编码 默认编码格式是UTF-8 , encoding="可以选择编码格式"
bs = name1.encode(encoding="utf-8")
- 解码
print(bs.decode(encoding="gbk"))
- 加入间隔符
name1 = "刘老根和药匣子.jpg"
print(",".join(name1))
#刘,老,根,和,药,匣,子,.,j,p,g
- 把字符串分成列表
print(name2.split("-")) #列表
#如果没有括号内的标识符,默认分为一个列表
- 把指定的字符串 同其他的字符串替换 替换次数
print(name1.replace("刘老根","大辣椒",1))
- 查询出现的次数
print(name1.count("刘老根",1))
- 去除字符串左右的 特定字符串 默认是空格
name1 = "ccccc刘老根和药匣子.jpgccccc"
print(name1.strip('c'))
#刘老根和药匣子.jpg
- 右对齐去除 默认空格
name1 = "ccccc刘老根和药匣子.jpgccccc"
print(name1.lstrip("c"))
#刘老根和药匣子.jpgccccc
- 左对齐去除 默认空格
print(name1.rstrip())
- format格式化
print('{} {}'.format('hello','world')) # 不带字段
2 hello world
3 >>> print('{0} {1}'.format('hello','world')) # 带数字编号
4 hello world
5 >>> print('{0} {1} {0}'.format('hello','world')) # 打乱顺序
6 hello world hello
7 >>> print('{1} {1} {0}'.format('hello','world'))
8 world world hello
>>> print('{a} {tom} {a}'.format(tom='hello',a='world')) # 带关键字
10 world hello world
- find查找 如果在字符串里找不到返回-1
# print(name1.find("国")) #返回-1
# print(name1.index("国")) #找不到报错
- 判断开头和结果字符串是否正确 返回布尔类型
print(name1.endswith(".jpg"))
print(name1.startswith("刘老根"))
16.进行字符串的补齐
name1 = "刘老根和药匣子"
print(name1.rjust(15,"*")) #以右对齐 一共要15个字节 缺少的用*在左侧补全
print(name1.ljust(10,"+")) #以左对齐 一共要10个字节 缺少的用+在右侧补齐
- 导入tushare
pip install tushare