-
编写一个程序,交换指定字典的key和value。
例如:dict1={'a':1, 'b':2, 'c':3} --> dict1={1:'a', 2:'b', 3:'c'} # 1 dict1={'a':1, 'b':2, 'c':3} dict2={} for key,value in dict1.items(): dict2[value]=key print(dict2) # 2 dict1={'a':1, 'b':2, 'c':3} dict2={value:key for key,value in dict1.items()} print(dict2)
-
编写一个程序,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'12a&bc12d-+' --> 'abcd' str1='12a&bc12d-+' newstr='' for char in str1: if 'a'<=char<='z' or 'A'<=char<='Z': newstr+=char print(newstr)
-
写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
例如: 'abc' -> 'Abc' '12asd' --> '12asd'
str1=input("请输入:") if 'a'<=str1[0]<='z': print(str1[0].upper()+str1[1:]) else: print(str1)
-
写程序实现endswith的功能,判断一个字符串是否已指定的字符串结束
例如: 字符串1:'abc231ab' 字符串2:'ab' 函数结果为: True 字符串1:'abc231ab' 字符串2:'ab1' 函数结果为: False
str1=input("请输入字符串1:") str2=input("请输入字符串2:") len1=len(str1) len2=len(str2) print(str1[len1-len2:]==str2)
-
写程序实现isdigit的功能,判断一个字符串是否是纯数字字符串
例如: '1234921' 结果: True '23函数' 结果: False 'a2390' 结果: False
str1=input("请输入:") for char in str1: if not '0'<=char <='9': print('False') break else: print('True')
-
写程序实现upper的功能,将一个字符串中所有的小写字母变成大写字母
例如: 'abH23好rp1' 结果: 'ABH23好RP1'
str1=input("请输入:") newstr='' for char in str1: if 'a'<=char<='z': newstr+=chr(ord(char)-32) else: newstr+=char print(newstr)
-
写程序获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
例如: 序列:[-7, -12, -1, -9] 结果: -1 序列:'abcdpzasdz' 结果: 'z' 序列:{'小明':90, '张三': 76, '路飞':30, '小花': 98} 结果: 98
seq={'小明':90, '张三': 76, '路飞':30, '小花': 98} # seq=[-7, -12, -1, -9] if type(seq)==dict: print(max(seq.values())) else: print(max(seq))
-
写程序实现replace函数的功能,将指定字符串中指定的旧字符串转换成指定的新字符串
例如: 原字符串: 'how are you? and you?' 旧字符串: 'you' 新字符串:'me' 结果: 'how are me? and me?'
# 方法一 先把第九题写出来,然后拼接 main_str= 'how are you? and you?' # 主串 result= '' # 新串 match_str= 'you' # 匹配串 replace_str= 'me' # 替换串 len1=len(main_str) len2=len(match_str) index_list=[-len2] # 下标列表,提前存一个,方便后续循环 str_list=[] # 放分割后子串的列表 for index in range(len1-len2+1) : # 获取每一个匹配串出现的位置 if main_str[index:index + len2]==match_str: index_list.append(index) for i in range(len(index_list)-1): start=index_list[i]+len2 # 分割开始下标 end=index_list[i+1] # 分割结束下标 result+= main_str[start:end] + replace_str else: result + main_str[end + len2:] print(result) # 方法二 main_str= 'how are you? and you?' # 主串 result= '' # 新串 match_str= 'you' # 匹配串 replace_str= 'me' # 替换串 len1=len(main_str) len2=len(match_str) start=0 end=len(match_str) while start<len1: if main_str[start:end]==match_str:# 相等就直接拼接上替换的串,让开始下标直接跳过匹配串 result+=replace_str start+=len2 end+=len2 else: #不相等,就只加一个当前开始下标的字符,然后让开始下标加1,从下一个字符继续比对 result+=main_str[start] start+=1 end+=1 print(result)
-
写程序实现split的功能,将字符串中指定子串作为切割点对字符串进行切割
例如:原字符串: 'how are you? and you?' 切割点: 'you' 结果: ['how are ', '? and ', '?']
main_str='how are you? and you?' match_str='you' result=[] len1=len(main_str) len2=len(match_str) index_list=[-len2] for index1 in range(len1-len2+1): if main_str[index1:index1+len2]==match_str: index_list.append(index1) for i in range(len(index_list)-1): start=index_list[i]+len2 # 分割开始下标 end=index_list[i+1] # 分割结束下标 result.append(main_str[start:end]) else: result.append(main_str[end+len2:]) print(result) # 2 main_str= 'how are you? and you?you' # 主串 result= [] # 新串 match_str= 'you' # 匹配串 replace_str= 'me' # 替换串 len1=len(main_str) len2=len(match_str) start=0 end=len(match_str) start1=0 # #切片的开始下标 while start<len1: if main_str[start:end]==match_str: result.append(main_str[start1:start]) start1=start+len2 start+=len2 end+=len2 else: start+=1 end+=1 else: result.append(main_str[start1:]) print(result)
day9-字符串作业(1)
最新推荐文章于 2024-09-16 14:49:33 发布
本文介绍了如何使用Python进行字符串操作,包括提取字母、首字母大写、判断是否为纯数字、转换为大写、实现endswith和isdigit功能。此外,还展示了如何交换字典的key和value,以及实现split和replace函数的功能。这些实例深入浅出地展示了Python字符串和字典的基本操作。
摘要由CSDN通过智能技术生成