# st = "这是一个字符串" # 字符串是一个有序的序列结构 # 序列结构---里面可以存储多个元素 # 有序---有下标(索引),我们可以通过下标或者索引对里面的元素进行一个获取 # https://www.17k.com/book/3583102.html 小说的地址 # href="//www.17k.com/book/3583102.html" 能拿到的 # res = 'href="//www.17k.com/book/3583102.html"' # 索引从0开始计数 # res1 = res[6:-2] # 结束位置是开区间,拿不到 # res1 = res[6:-1] # 结束位置是开区间,拿不到 # print(res1) # res3 = "https:" # 将两个字符串变成同一个:字符串的拼接 str + str # url = res3 + res1 # print(url) """ str.replace("新“, "旧") 替换 str.split("指定的字符") 以指定改的字符对字符串进行切割 str.find("字符") 查找这个字符在字符串里面的位置,如果没有,返回-1 """ # title = "必看好房,金茂悦,实验,师大 博才,近地铁,文化艺术中心" # # print(title.split(",")) # # print(title.split(",")[1]) # # print(title.split(",")[3]) # # sd = title.split(",")[3] # '师大 博才' # # 将 '师大 博才' 中间的空格取消 空格替换为空字符 # print(sd.replace(" ", "")) # print(title.find("好")) # print(title.find("紫")) """ 字符串的其他方法 sub表示的是我们指定的字符 index(sub) 和find()函数功能一样,但是sub不在字符串中会报错 count(sub) 放回sub在字符串中出现的次数 capitalize() 讲字符串第一个字符装换为大写 title() 所有单词首字母大写 upper() 小写字母变大写 lower() 大写字母变小写 swapcase() 字母大小写互换 isalpha() 判断字符串中元素是否全是字母 isdigit() 判断字符串中元素是否全是数字 strip() 删除字符串首尾空格 center(width,fillchar) width指定的是字符串的长度,位置居中的字符串,fillchar填充 """ # st = "hello world liumu" # print(st.count("l")) # 3 # print(st.count("o")) # 2 # print(st.capitalize()) # print(st.title()) # print(st.isdigit()) # False # print("28".isdigit()) # True # # print(st.isalpha()) # False # print("我是C位".center(10)) # 将当前的字符放在中心,两边不够的地方用空格补上 # print("我是C位".center(10, "*")) # print("我是C位".center(11, "*")) # 奇数就前面多一个 """ 字符串转义 \n 换行符 \t 制表符 r 取消转义 """ # print('志忠 生如夏花') # # print('志忠\n生如夏花') # print("wj 生如夏花") # print("Eter 生如夏花") # print("*" * 20) # print("wj\t\t生如夏花") # print('志忠\t\t生如夏花') # print("Eter\t生如夏花") # print('D:\流木\name') # \n表示的是换行 # # 如果想要保留 \n 原本的字符串含义,就需要取消转义 # # 方法一: 在字符串最前面加上一个 r"" # print(r'D:\流木\name') # D:\流木\name # # 方法二:在转义符前面加上一个 \ # print('D:\流木\\name') # D:\流木\name """ 字符串编码方式 编码 str.encode("编码方式") 解码 str.decode("解码方式") """ jun = "俊" # jun_en = jun.encode() # b'\xe4\xbf\x8a' 默认就是utf-8 jun_en = jun.encode('utf-8') # b'\xe4\xbf\x8a' print(jun_en) print(jun_en.decode()) # 俊 utf-8 # print(jun_en.decode('ASCII')) # 解码和编码不一致,报错 print(jun_en.decode('gbk')) # 解码和编码不一致,报错
09 字符串补充
最新推荐文章于 2024-06-29 16:57:48 发布