-
编写一个程序,交换指定字典的key和value。
例如:dict1={'a':1, 'b':2, 'c':3} --> dict1={1:'a', 2:'b', 3:'c'} print('-------------------------[1]-------------------------') dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {} l1 = list(dict1) l2 = list(dict1.values()) for x in range(len(dict1)): dict2.setdefault(l2[x], l1[x]) print(dict2)
-
编写一个程序,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'12a&bc12d-+' --> 'abcd' print('-------------------------[2]-------------------------') s1 = '12a&bc12d-+' s2 = '' for x in range(len(s1)): if 'a' <= s1[x] <= 'z' or 'A' <= s1[x] <= 'Z': s2 += s1[x] print(s2)
-
写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
例如: 'abc' -> 'Abc' '12asd' --> '12asd' print('-------------------------[3.capitalize()]-------------------------') s1 = 'a123abc' s2 = '' for x in range(len(s1)): if x == 0 and 'a' <= s1[x] <= 'z': s2 += chr(ord(s1[x]) - 32) else: s2 += s1[x] print(s2)
-
写程序实现endswith的功能,判断一个字符串是否已指定的字符串结束
例如: 字符串1:'abc231ab' 字符串2:'ab' 函数结果为: True 字符串1:'abc231ab' 字符串2:'ab1' 函数结果为: False print('-------------------------[4.endswith()]-------------------------') s1 = 'abc123ab' s2 = 'ab' for x in range(len(s2), 0, -1): if s1[-x] != s2[-x]: print(0 > 1) break else: print(1 > 0)
-
写程序实现isdigit的功能,判断一个字符串是否是纯数字字符串
例如: '1234921' 结果: True '23函数' 结果: False 'a2390' 结果: False print('-------------------------[5.isdigit()]-------------------------') s1 = '123492' for x in range(len(s1)): if not '0' <= s1[x] <= '9': print(0 > 1) break else: print(1 > 0)
-
写程序实现upper的功能,将一个字符串中所有的小写字母变成大写字母
例如: 'abH23好rp1' 结果: 'ABH23好RP1' print('-------------------------[6.upper()]-------------------------') s1 = 'abH23好rp1' s2 = '' for x in range(len(s1)): if 'a' <= s1[x] <= 'z': s2 += chr(ord(s1[x]) - 32) else: s2 += s1[x] print(s2)
-
写程序获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
例如: 序列:[-7, -12, -1, -9] 结果: -1 序列:'abcdpzasdz' 结果: 'z' 序列:{'小明':90, '张三': 76, '路飞':30, '小花': 98} 结果: 98 print('-------------------------[7]-------------------------') s1 = eval(input('输入序列:')) if type(s1) == dict: l1 = list(s1.values()) print(max(l1)) else: print(max(s1))
-
写程序实现replace函数的功能,将指定字符串中指定的旧字符串转换成指定的新字符串
例如: 原字符串: 'how are you? and you?' 旧字符串: 'you' 新字符串:'me' 结果: 'how are me? and me?' print('-------------------------[8.replace]-------------------------') s1 = 'how are you? and you?' s2 = 'you' s3 = 'me' s4 = '' z = 0 for x in range(len(s1)): if z < len(s1): if s1[z] == s2[0]: for y in range(len(s2)): if s1[z + y] != s2[y]: break else: s4 += s3 z += len(s2) else: s4 += s1[z] z += 1 else: break print(s4)
-
写程序实现split的功能,将字符串中指定子串作为切割点对字符串进行切割
例如:原字符串: 'how are you? and you?' 切割点: 'you' 结果: ['how are ', '? and ', '?'] print('-------------------------[9.split()]-------------------------') s1 = 'how are you? and you?' s2 = 'you' l1 = [] a, b = 0, 0 for x in range(len(s1)): if b + len(s2) <= len(s1): if s1[b] == s2[0]: for y in range(len(s2)): if s1[b + y] != s2[y]: b += 1 break else: l1.append(s1[a:b]) b += len(s2) a = b elif b == (len(s1) - 1) and a != b: l1.append(s1[a:b + 1]) break elif b == (len(s1) - 1) and a == b: l1.append(s1[b]) break else: b += 1 elif b < len(s1) and b + len(s2) > len(s1): if b == (len(s1) - 1) and a != b: l1.append(s1[a:b + 1]) break elif b == (len(s1) - 1) and a == b: l1.append(s1[b]) break else: b += 1 else: break print(l1)
-
用思维导图(xmind)总结四大容器:列表、字典、元组、集合
day10-字符串作业2
于 2023-03-18 16:43:30 首次发布