def count_fuc(str): count_number=0 count_a=0 count_b=0 for i in str: if i.isdecimal(): count_number+=1 elif i.isalpha(): count_a+=1 else: count_b+=1 return print("数字:%s,字母:%s,其他:%s"%(count_number,count_a,count_b)) str="fdsfdsf322@" count_fuc(str) ### 数字:3,字母:7,其他:1
eg:传入的列表为[1,2,3,4],返回的字典为{0:1,1:2,2:3,3:4}
def fun(list1): dict1={} for i in range(len(list1)): dict1[i]=list1[i] return dict1 list1=[1,2,3,4] print(fun(list1))
此函数接受这个这个内容并追加到student_messgae.tx
t这个文本中,支持用户持续输入,Q或者q退出
''' 学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:725638078 寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书! ''' def fuc(name,sex,age,qualifications): with open("student_message.txt",encoding="utf-8",mode="a") as f: f.write("%s,%s,%s,%s\n"%(name,sex,age,qualifications)) while True: name=input("please input your name:") if name.upper()=="Q": break else: sex=input("please input your sex:") age = input("please input your age:") qualifications= input("please input your qualifications:") fuc(name,sex,age,qualifications)
import os def refile(file1,oldcontent,newcontent): with open(file1,encoding="utf-8") as f,\ open("new{}".format(file1),encoding="utf-8",mode="w") as f1: for line in f: content=line.replace(oldcontent,newcontent) f1.write(content) # old_content=f.read() # new_content=old_content.replace(oldcontent,newcontent) # f1.write(new_content) os.remove(file1) os.rename("new{}".format(file1),file1) refile("student_message.txt","房贷首付","没钱")
结尾给大家推荐一个非常好的学习教程,希望对你学习Python有帮助!
Python基础入门教程推荐:更多Python视频教程-关注B站:Python学习者
Python爬虫案例教程推荐:更多Python视频教程-关注B站:Python学习者