1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| """ 查找:x.find(y) 在字符串x中查找y,如果找到,返回第一次找到对应的索引值, 如果没有找到,返回-1 text = " good good study day day up" num = text.find("study1") print(num)
x.index(y): 在字符串x中查找y, 如果找到,返回第一次找到的索引,如果找不到, 报错 ValueError text = " good good study day day up" num = text.index("study1") print(num)
替换: x.replace(y,z), 把字符串x中的y替换为z, 替换完之后返回一个新的字符串, 原来的字符串不变 x = "I am good boy" text2 = x.replace("good","bad") print(text2)
字符串分割:x.split([y]), 把字符串x按照y进行分割 字符串合并: y.join(x), 用字符串y将x中的每个数据进行连接 去除空格: x.strip(), 把字符串x两边的空格去掉 编码: x.encode("utf-8") 把字符串x编码为字节数据,参数为编码规则,默认为utf-8 解码: x.decode("utf-8") 把字节数据x解码为字符串,参数为解码规则,同编码一致 """
x = "你好,谢谢,对不起,请,再见" text3 = x.split(",") print(text3)
text4 = "%4%".join(text3) print(text4)
x = " wolf code, code color " y = x.strip() print(x) print(y)
print("="*50) x = "hello,你好" y = x.encode("utf-8") #b开头 print(x) print(y) print(type(x)) print(type(y)) text5 = y.decode() print(text5)
|