必做题
1.输入一个字符串,判断字符串中有多少个字母?多少个数字?多少个其他符号
例如:'hello, nice to meet you. i am 18. my birthday is 1999-05-23'
-- 结果: 字母的个数为33个,数字个数为10个, 其他字符为16个
a="hello, nice to meet you. i am 18. my birthday is 1999-05-23"
countstr=0
countnum=0
countelse=0
for i in a:
if i.isalpha():
countstr+=1
elif i.isdigit():
countnum+=1
else:
countelse+=1
print("字母的个数为%d个,数字的个数为%d个,其他字符为%d个" % (countstr,countnum,countelse))
2.以下是一段歌词,请从这段歌词中统计出朋友出现的次数。
“这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。”
b="这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。"
print(b.count("朋友"))
3.编写敏感词过滤程序
说明:在网络程序中,如聊天室、聊天软件等,经常需要对一些用户所提交的聊天内容中的敏感性词语进行过滤。如“性”、“色情”、“爆炸”、“恐怖”、“枪”、“军火”等,这些都不可以在网上进行传播,要求输入一段文本,如果包含以上的敏感词汇,需要*替换掉
例如:“军火走私” ---- 结果为 "**走私"
text=input("请输入要过滤的文字")
#text="我枪王爆炸猛"
if "性" in text:
text=text.replace("性","*")
if "色情" in text:
text = text.replace("色情", "*")
if "爆炸" in text:
text = text.replace("爆炸", "*")
if "军火" in text:
text = text.replace("军火", "*")
if "恐怖" in text:
text = text.replace("恐怖", "*")
if "枪" in text:
text = text.replace("枪", "*")
print(text)
#或者
wordlist = ["性","色情","爆炸","恐怖","枪","军火"]
data=input("请输入内容:")
for word in wordlist:
if word in data:
data = data.replace(word,"*" * len(word))
print(data)
4.输入一个用户名,判断用户名是否合法。用户名要求:由英文字母或数字组成,长度是6到12位
例如:‘abcd’ --- 不合法 "123456" -- 合法 ‘ABC23’ -- 不合法 ‘ABC123’ -- 合法
“abc 124” --- 不合法
text=input("请输入测试的用户名:")
# text="a77771"
if text.isalnum()==True and 6<=len(text)<=12:
print("合法")
else:
print("不合法")
#或者
test=input("请输入测试的用户名:")
# text="a77771"
if 6<=len(test)<=12:
for ch in test:
#isalpha 判断字母,注意:出现ACSII码中字符以外的结果都是Ture
if not(ch.isdigit() or ch.isalpha()):
print("不合法")
break
else:
print("合法")
else:
print("长度不合法")
#或者 这个最好,不会出现歧义
test=input("请输入测试的用户名:")
# text="a77771"
if 6<=len(test)<=12:
for ch in test:
#isalpha 判断字母,注意:出现ACSII码中字符以外的结果都是Ture
if not("a"<=ch<="z" or"A"<=ch<="Z" or "0"<=ch<="9" ):
print("不合法")
break
else:
print("合法")
else:
print("长度不合法")
5.随机生成长度为5的验证码, 验证码的组成是英文字母或者数字
import random
str1="abcdefghijklmnopqrstuvwsyzQWERTYUIOPASDFGHJKLZXCVBNM0123456789"
num=""
for i in range(5):
num+=random.choice(str1)
print(num)
6.实现将字符串 “1,2,3” 变成列表 [“1”,“2”,“3”]
str1="1,2,3".replace(",","")
print(list(str1))
7.判断输入的字符串是否是 .py 结束
str1=input("请输入字符串")
print(str1)
#str1="asdff.py"
if str1[-3:]==".py":
print("是以.py结尾")
8.写出你对今日授课内容中有疑问的地方(或者觉得有困难的知识点)
选做题
1.输入一个字符串,将字符串中所有的数字符取出来产生一个新的字符串
例如: 输入: 'abc1shj23kls99+2kkk' 输出: '123992'
a='abc1shj23kls99+2kkk'
b=""
for i in a:
if i.isdigit():
b+=i
print(b)
2.输入用户名,判断用户名是否合法,用户名的要求:必须由数字和字母且只能有数字和字母,并且第一个字符是大写字母
例如: 'Abc' — 不合法 '123' — 不合法 'abc123' — 不合法 'Abc123ahs' — 合法
test=input("请输入测试的用户名:")
str1="QWERTYUIOPASDFGHJKLZXCVBNM"
if test[0] in str1:
for ch in test:
#isalpha 判断字母,注意:出现ACSII码中字符以外的结果都是Ture
if not("a"<=ch<="z" or"A"<=ch<="Z" or "0"<=ch<="9" ):
print("不合法")
break
else:
print("合法")
else:
print("不合法")
3.输入两个字符串,打印两个字符串中公共的字符,如果没有公共字符打印 公共字符不存在
例如: 字符串1为 abc123a , 字符串2为 huak33 , 打印 a3
a="abc123a"
b="huak33"
all_str=a+b
str_dict={}
for i in all_str:
if i not in str_dict:
str_dict[i]=1
else:
str_dict[i] += 1
print(str_dict)
count_dict={}
#print(len(count_dict))
for k,v in str_dict.items():
if v>=2:
count_dict[k]=v
if len(count_dict)==0:
print("公共字符不存在")
else:
print(count_dict)
4.如下字符串: “01#张三#60-02#李四#90-03#王五#70”, 每一部分表示 学号#姓名#分数,提取学生信息存放于列表中,并按照成绩对学生降序排序
结果显示为:
[{"学号":'02', '姓名':'李四', '分数':90}, {"学号":'03', '姓名':'王五', '分数':70}, {"学号":'01', '姓名':'张三', '分数':60}]
a="01#张三#60-02#李四#90-03#王五#70"
list1=a.split("-")
print(list1)
list3=[]
for i in list1:
list2=i.split("#")
dict1=dict(zip(["学号","姓名","分数"],list2))
list3.append(dict1)
print(list3)
for i in range(len(list3)-1):
for j in range(len(list3)-1-i):
if list3[j]["分数"]<list3[j+1]["分数"]:
list3[j],list3[j+1]=list3[j+1],list3[j]
print(list3)