1.实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
#将所有,.?!等替换为空格 m='''To be human is to love Even when it gets too much I'm not ready to give up To be human is to love Even when it gets too much I'm not ready to give up To be human To be human To be human Just 'cause I predicted this Doesn't make it any easier to live with And what's the point of knowin' it If you can't change it you can't change can't change it Just 'cause I predicted this Doesn't make it any easier to live with AnUnder rich relentless skies I've been setting highs I felt you walk right through me You're the thing that I invoke My all persistent goal Sent to make me queazy And oh it's hard now With time it works out To be human is to love Even when it gets too much I'm not ready to give up To be human is to love Even when it gets too much I'm not ready to give up All the tigers have been out I don't care I hear them howl I let them tear right through me Can you help me not to care Every breath becomes a prayer Take this pain from me And oh you're so far now So far from my arms now To be human is to love Even when it gets too much I'm not ready to give up To be human is to love Even when it gets too much I'm not ready to give up To be human To be human To be human Just 'cause I predicted this Doesn't make it any easier to live with And what's the point of knowin' it If you can't change it you can't change can't change it Just 'cause I predicted this Doesn't make it any easier to live with And what's the point of knowin' it If you can't change it you can't change can't change it To be human is to love Even when it gets too much I'm not ready to give up To be human is to love Even when it gets too much There's no reason to give up Don't give up Don't give up''' m=print(m.replace(',',' ')) m=print(m.replace('.',' ')) m=print(m.replace('?',' ')) m=print(m.replace('!',' '))

#全部替换为小写 m='''To be human is to love Even when it gets too much I'm not ready to give up To be human is to love Even when it gets too much I'm not ready to give up To be human To be human To be human Just 'cause I predicted this Doesn't make it any easier to live with And what's the point of knowin' it If you can't change it you can't change can't change it Just 'cause I predicted this Doesn't make it any easier to live with AnUnder rich relentless skies I've been setting highs I felt you walk right through me You're the thing that I invoke My all persistent goal Sent to make me queazy And oh it's hard now With time it works out To be human is to love Even when it gets too much I'm not ready to give up To be human is to love Even when it gets too much I'm not ready to give up All the tigers have been out I don't care I hear them howl I let them tear right through me Can you help me not to care Every breath becomes a prayer Take this pain from me And oh you're so far now So far from my arms now To be human is to love Even when it gets too much I'm not ready to give up To be human is to love Even when it gets too much I'm not ready to give up To be human To be human To be human Just 'cause I predicted this Doesn't make it any easier to live with And what's the point of knowin' it If you can't change it you can't change can't change it Just 'cause I predicted this Doesn't make it any easier to live with And what's the point of knowin' it If you can't change it you can't change can't change it To be human is to love Even when it gets too much I'm not ready to give up To be human is to love Even when it gets too much There's no reason to give up Don't give up Don't give up''' m=str.lower(m) print(m)

#统计某单词出现次数 m='''to be human is to love even when it gets too much i'm not ready to give up to be human is to love even when it gets too much i'm not ready to give up to be human to be human to be human just 'cause i predicted this doesn't make it any easier to live with and what's the point of knowin' it if you can't change it you can't change can't change it just 'cause i predicted this doesn't make it any easier to live with anunder rich relentless skies i've been setting highs i felt you walk right through me you're the thing that i invoke my all persistent goal sent to make me queazy and oh it's hard now with time it works out to be human is to love even when it gets too much i'm not ready to give up to be human is to love even when it gets too much i'm not ready to give up all the tigers have been out i don't care i hear them howl i let them tear right through me can you help me not to care every breath becomes a prayer take this pain from me and oh you're so far now so far from my arms now to be human is to love even when it gets too much i'm not ready to give up to be human is to love even when it gets too much i'm not ready to give up to be human to be human to be human just 'cause i predicted this doesn't make it any easier to live with and what's the point of knowin' it if you can't change it you can't change can't change it just 'cause i predicted this doesn't make it any easier to live with and what's the point of knowin' it if you can't change it you can't change can't change it to be human is to love even when it gets too much i'm not ready to give up to be human is to love even when it gets too much there's no reason to give up don't give up don't give up''' print(m.count('human'))

#分隔单词 m='''to be human is to love even when it gets too much i'm not ready to give up to be human is to love even when it gets too much i'm not ready to give up to be human to be human to be human just 'cause i predicted this doesn't make it any easier to live with and what's the point of knowin' it if you can't change it you can't change can't change it just 'cause i predicted this doesn't make it any easier to live with anunder rich relentless skies i've been setting highs i felt you walk right through me you're the thing that i invoke my all persistent goal sent to make me queazy and oh it's hard now with time it works out to be human is to love even when it gets too much i'm not ready to give up to be human is to love even when it gets too much i'm not ready to give up all the tigers have been out i don't care i hear them howl i let them tear right through me can you help me not to care every breath becomes a prayer take this pain from me and oh you're so far now so far from my arms now to be human is to love even when it gets too much i'm not ready to give up to be human is to love even when it gets too much i'm not ready to give up to be human to be human to be human just 'cause i predicted this doesn't make it any easier to live with and what's the point of knowin' it if you can't change it you can't change can't change it just 'cause i predicted this doesn't make it any easier to live with and what's the point of knowin' it if you can't change it you can't change can't change it to be human is to love even when it gets too much i'm not ready to give up to be human is to love even when it gets too much there's no reason to give up don't give up don't give up''' print(m.split(' '))

2.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
n=str('12312312312312') print('作业评分列表:'+n) s=n.index('3') print('第一个3分的下标:',s) a=n.count('1') print('统计1分的同学有:',a) b=n.count('3') print('统计3分的同学有:',b)

3.简要描述列表与元组的异同。
列表list是一种有序的序列,正向递增,反向递减。可以随时添加和删除其中的元素。没有长度限制,元素类型可以不同。
元组tuple是不可变的,即你不能修改元组。元组通过圆括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。
异:列表可以随时修改,元组初始化之后则不能修改。
同:两者较为相似。
浙公网安备 33010602011771号