-
编写一个程序,交换指定字典的key和value。
例如:dict1={'a':1, 'b':2, 'c':3} --> dict1={1:'a', 2:'b', 3:'c'} dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = dict([(value, key) for key, value in dict1.items()]) print(dict2) # {1: 'a', 2: 'b', 3: 'c'}
-
编写一个程序,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'12a&bc12d-+' --> 'abcd' str1 = 'e3r4ghn09345jv0938sh348' str2 = '' for i in str1: if 'a' <= i <= 'z' or 'A' <= i <= 'Z': str2 += i print(str2)
-
写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
例如: 'abc' -> 'Abc' '12asd' --> '12asd' str1 = 'abd2fbs32' str2 = '' for i in range(len(str1)): if 'a' <= str1[0] <= 'z': str2 = chr(ord(str1[0]) - 32) str2 += str1[1:] print(str2) break else: print(str1)
-
写程序实现endswith的功能,判断一个字符串是否已指定的字符串结束
例如: 字符串1:'abc231ab' 字符串2:'ab' 函数结果为: True 字符串1:'abc231ab' 字符串2:'ab1' 函数结果为: False str1 = input('请输入第一个字符串:') str2 = input('请输入你想要的判断的字符串:') str3 = len(str2) if str1[0-str3::] == str2: print('str1是以str2结束') else: print('str1不是以str2结束')
-
写程序实现isdigit的功能,判断一个字符串是否是纯数字字符串
例如: '1234921' 结果: True '23函数' 结果: False 'a2390' 结果: False str1 = input('请输入一个要进行判断的字符串:') for i in str1: if not('0' <= i <= '9'): print('str1不是纯数字字符串') break else: print('str1是纯数字字符串')
-
写程序实现upper的功能,将一个字符串中所有的小写字母变成大写字母
例如: 'abH23好rp1' 结果: 'ABH23好RP1' str1 = 'Dg 834hb`u4/猪7vb' str2 = '' for i in str1: if 'a' <= i <= 'z': str2 += chr(ord(i) - 32) else: str2 += i print(str2)
-
写程序获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
例如: 序列:[-7, -12, -1, -9] 结果: -1 序列:'abcdpzasdz' 结果: 'z' 序列:{'小明':90, '张三': 76, '路飞':30, '小花': 98} 结果: 98 str1 = input('请输入一个序列:') l1=[] if type(eval(str1))==dict: d1=eval(str1) for i in d1: l1.append(d1[i]) print(max(l1)) elif type(eval(str1))==list: li=eval(str1) print(max(li)) else: print(max(eval(str1)))
-
写程序实现replace函数的功能,将指定字符串中指定的旧字符串转换成指定的新字符串
例如: 原字符串: 'how are you? and you?' 旧字符串: 'you' 新字符串:'me' 结果: 'how are me? and me?' a = input('输入字符串:') b = input('目标字符:') c = input('替换内容:') d = str() i = 0 while i < len(a): if a[i] == b[0] and b == a[i:i+len(b)]: d += c i += len(b) else: d += a[i] i += 1 print(d)
-
写程序实现split的功能,将字符串中指定子串作为切割点对字符串进行切割
例如:原字符串: 'how are you? and you?' 切割点: 'you' 结果: ['how are ', '? and ', '?'] 例如:原字符串: 'how are you? and you?' 切割点: 'you' 结果: ['how are ', '? and ', '?'] a = input('输入字符串:') b = input('目标字符:') d = [] i = 0 index = 0 while i != len(a): if a[i] == b[0] and b == a[i:i+len(b)]: d.append(a[index:i]) i += len(b) index = i else: i += 1 d.append(a[index::]) print(d)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交