字符串的应用 str0 = "abc" str1 = "adc" res = str0 + str1 print(res) print(str0) list0 = [12, 34, 56] print(list0[1]) list0[1] = 23 print(list0) str0 = "hello nice to meet you" print(str0[0]) #str0[0] = "H" TypeError: 'str' object does not support item assignment print(str0) #获得序列的长度 # 内置方法 len(序列) res = len(str0) print(res) #获得指定索引位置的字符 # 索引的取值范围 [0, len(str0) - 1] # #也可以通过负值来获取 [-len(str0), -1] #res = str0[len(str0)] #IndexError: string index out of range res = str0[4] print(res) res = str0[-1] print(res) #获得子串 ---- 切片 ''' 切片语法: 序列[start:stop:step] step 值默认为1 start 和 stop不传值 默认是从头到尾 ''' sub_str = str0[::] print(sub_str) #跳跃取值 使用步长 sub_str = str0[::2] print(sub_str) #只传起始位置 默认是从起始位置到最后 sub_str = str0[1::] print(sub_str) #不传起始位置 只传结束位置 [0, stop) sub_str = str0[:3:] print(sub_str) #[start, stop) sub_str = str0[1:3:] print(sub_str) #反转 reverse = str0[::-1] print(reverse) str0 = "hello nice to meet you too" #获得某个子串第一次出现的位置索引 找不到返回-1 #如果查找的是连续的字符序列 返回的是第一个字符对应的索引位置 #从左向右找 第一次出现的位置 index = str0.find("to") print(index) #指定起始位置进行查找 index = str0.find("to", 12) print(index) #指定区间进行查找 index = str0.find("to", 12, 20) print(index) #从右向左找 指定字符串第一次出现在整个字符串的位置 index = str0.rfind("to") print(index) #类似find的方法 区别: 这个是找不到报错 ValueError: substring not found # index = str0.index("mo") # print(index) str0 = "hello nice to meet you too" #统计字符串中某个子串出现的次数 : 数过了不用管了 继续向下数 num = str0.count("o") print(num) str0 = "kkeekkkfabckkkkk" num = str0.count("kk") print(num) #从某个位置开始数 num = str0.count("kk", 5) print(num) #指定区间 num = str0.count("kk", 5, 10) print(num) #切割字符串: 根据指定的切割符位置 对字符串进行分割 返回的是列表 来保存被分割出来的子串 str0 = "hello nice to meet you too\nmeet you too" sub_list = str0.split() print(sub_list) #不指定切割符 默认以空白为切割符进行切割 sub_list = str0.split("h") print(sub_list) #设置切割次数 sub_list = str0.split(maxsplit=3) print(sub_list) def cus_count(src, sep): sub_list = src.split(sep) print(sub_list) return len(sub_list) - 1 res = cus_count("kkeekkkfabckkkkk", "kk") print(res) # 去除掉字符串两端指定内容 str0 = " abc abc abc " print(str0) res = str0.strip() #不传去除的内容默认去除的是空白 print(res) str0 = "[12, 34, 56]" res = str0.strip("[") print(res) res = res.strip("]") print(res) str0 = "123abcd123" res = str0.strip("123") print(res) #只去除左边 res = str0.lstrip("123") print(res) #只去除右边 res = str0.rstrip("123") print(res) ''' 字符串格式化: 2 ---> 02 3.14 ---> 3.1 ''' res = "%02d %.1f" % (2, 3.14) print(res) ''' 转义符 \ \n ----> 换行 想显示\的原本状态 就\进行转义 转义成本身的含义 -(-10) = 10 ''' res = "m\\nn\\t\\r" print(res) #r的修饰 可以保持字符串内容的本意 res = r"m\nn\t\r" print(res) res = r"c:\Documents\讲课内容\BJ_Python1811\day05\notes" print(res) #想显示的字符串内容 你好 ---> "hello" res = '你好 ---> "hello"' print(res) res = "你好 ---> \"hello\"" print(res) #判断的方式 str0 = "testpngAV" #1. 查看字符串是否以指定内容开头 res = str0.startswith("t") print(res) #2.查看字符串是否以指定内容结尾 res = str0.endswith("test.png") print(res) #3. 判断字符串的内容是否是纯英文字母 res = str0.isalpha() print(res) #4. 判断字符串的内容是否是纯数字 str0 = "10" res = str0.isdigit() print(res) #转换的操作 str0 = "testpngAV12" #1. 将小写字母转换为大写字母 res = str0.upper() print(res) #2. 将大写英文字母转换为小写 res = str0.lower() print(res) #3. 将字符串中大写转化为小写 小写转换为大写 其他保持不变 res = str0.swapcase() print(res) #4. 将首字母转化为大写 res = str0.capitalize() print(res) #5. 将每个单词的首字母转换为大写 [单词和单词之间使用空格隔开] str0 = "Hello nice to Meet you" res = str0.title() print(res) #替换 str0 = "box boxes boxes Boss" res = str0.replace("b", "B", 2) print(res) #拼接 以指定拼接符拼接序列中的内容 list0 = ["10", "20", "30", "40"] # 10-20-30-40 res = "-".join(list0) print(res) #对字符串进行编码和解码 # 编码: 按照指定的编码方式 获得字符所占用的字节数据 b''-- 表示字节数据 # 解码: 按照指定的解码方式 获得字符数据对应的字符 res = "你好".encode("utf-8") print(res) #将字节数据解码成对应的字符 res = b'\xe4\xbd\xa0'.decode("utf-8") print(res) res = "你好".encode("gbk") print(res) #将字节数据解码成对应的字符 res = b'\xc4\xe3'.decode("gbk") print(res) ''' 在指定字符串中 查找某个字符第一次出现的位置 ''' # def cus_find_single(src, ch): # for i in range(len(src)): # if src[i] == ch: # return i # # return -1 #从指定位置开始在指定的字符串中查找某个字符第一次出现的位置 # def cus_find_single(src, ch, start = None, end = None): # #None ---> 0 not None --> start # cus_index = 0 if start == None else start # cus_end = len(src) if end == None else end # for i in range(cus_index, cus_end): # if src[i] == ch: # return i # # return -1 # # index = cus_find_single("hello nice", "e", 2, 4) # print(index) ''' 在指定字符串中 查找某个字串第一次出现的位置 ''' # for i in src: # pass # 空白站位符 被包含语句中如果没有代码 先可以使用pass站位 # def cus_find(src, key, start = None, end = None): # #记录位置的索引 # index = 0 if start == None else start # end_index = len(src) if end == None else end # while True: # #根据索引和子串的长度 从头开始一段一段的提取 # sub = src[index:index + len(key)] # if sub != key: # #继续找 # index += 1 # if index > (end_index - len(key)): # return -1 # else: # return index # # # res = cus_find("hello nice to meet you too", "to", 12, 20) # print(res) # for i in "abcdefgefgabcdefg": # print(i) ''' abcdefgefgabcdefg efg a ~ c [0, len("efg")) --> [0, 3) 0 : len("efg") abc bcd 123 [1, 4) cde 234 [2, 5) def [3, 6) efg [4, 7) efg abcmopq [0, 3) [1, 4) [2, 5) [3, 6) [4, 7) [5, 8) index > len(src) - len(key) ''' def cus_upper(src): new_str = "" for i in src: value = ord(i) if value >= 97 and value <= 122: upper_ch = chr(value - 32) new_str += upper_ch else: new_str += i return new_str res = cus_upper("abchelloOK") print(res) def cus_capitalize(src): new_str = "" for i in range(len(src)): value = ord(src[i]) if i == 0 : if value >= 97 and value <= 122: new_str += chr(value - 32) else: new_str += src[i] else: if value >= 65 and value <= 90: new_str += chr(value + 32) else: ord(i[0]) return new_str res = cus_capitalize("abceTEST") print(res) def cus_title(src): sub_list = src.split() new_str = "" # 遍历列表 for i in range(len(sub_list)): if i != len(sub_list) - 1: new_str += cus_capitalize(sub_list[i]) new_str += " " else: new_str += cus_capitalize(sub_list[i]) return new_str res = cus_title("Hello nice to Meet you") print(res) 数字方法 ''' 函数可以当做值被进行传递 ''' a = 10 print(a) #将函数当做值来进行传递时 不要函数明后面添加小括号 ''' 添加上小括号之后 相当于调用函数 把函数的运行结果赋予给执行的变量 所以只需赋予函数名即可 这样的话 对应的变量也具有函数的功能 ''' f = print print(10) f(100) #abs -- 求绝对值 res = abs(-100) print(res) #忽略正负的情况下获得最大值 ''' key接受的是一个函数 将每一个数据传入到函数中 返回进行比较大小的那种数据 ''' def cus_abs(num): if num < 0: return -num else: return num res = max(-10, -55, 77, 99, -101, key=cus_abs) print(res) # 求最小值 res = min(-10, 22, 33, -8, 99, 45) print(res) # 四舍五入 help(round) res = round(3.5415926) print(res) #这种情况下 默认是不保留小数的 res = round(3.5415926, 3) print(res) # 求幂数 x的y次方 res = 10 ** 2 print(res) res = pow(10, 2) print(res) # 导入模块 如果导入时 显示红色下划线 在file-settings-project - project interpreter -- 开发环境 import math print(math.pi) #向上求整 -- 大于等于数据的最接近的整数 res = math.ceil(18.1) print(res) #向下求整 -- 小于等于数据的最接近的整数 res = math.floor(18.9) print(res) #开平方 res = math.sqrt(8) print(res)