字符串练习

字符串练习:

http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html

取得校园新闻的编号

s = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html'
ns = s.rstrip('.html').split('_')
print(ns[1])

https://docs.python.org/3/library/turtle.html

产生python文档的网址

s1 = 'https://docs.python.org/3/library/'
s2 = '.html'
print s1+'tutle'+s2

http://news.gzcc.cn/html/xiaoyuanxinwen/4.html

产生校园新闻的一系列新闻页网址

s1 = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
s2 = '.html'
for i in range(2,10):
    print (s1+'i'+s2)

或者

for i in range(2,10):
    print ((s1+'{}'+s2).format('i'))

练习字符串内建函数:strip,lstrip,rstrip,split,count

用函数得到校园新闻编号

s = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0107/8933.html'
ns = s.rstrip('.html').split('_')
print ns[1]

用函数统计一歌词中单词出现的次数

song = '''
Seems like everybody's got a price 看似人人都有身价
I wonder how they sleep at night 我怀疑他们晚上如何安睡
When the tale comes first 地位权利如此来去快捷
And the truth comes second 而真相随后才到
Just stop, for a minute and 停下来 缓解一会儿
Smile 和微笑着
Why is everybody so serious 为何人人都杞人忧天 
Acting so damn mysterious 一副神神秘秘的样子
You got your shades on your eyes 眼中的灰暗如何剔除
And your heels so high 又穿这么高的高跟鞋
That you can't even have a good time 那活该你倒霉
Everybody look to their left (yeah) 人人向左看看(耶)
Everybody look to their right (ha) 人人向右看看(哈)
Can you feel that (yeah) 难道感觉不到吗(耶)
Well pay them with love tonight… 爱能让你今晚圆满...
It's not about the money, money, money 这无关金钱 金钱 金钱
We don't need your money, money, money 我们也不需要人们的金钱 金钱 金钱
We just wanna make the world dance 我只要世人跟我手舞足蹈
Forget about the Price Tag 共同忘掉身价牌
Ain't about the (uh) Ka-Ching Ka-Ching 不是关于(哼)金钱 金钱
Aint about the (yeah) Ba-Bling Ba-Bling 也不是关于(耶)穿金戴银
Wanna make the world dance 只想让世人共舞
Forget about the Price Tag 共同忘掉身价牌
We need to take it back in time 我们想要让时光轮回
When music made us all UNITE! 音乐让我们众志成城!
And it wasn't low blows and video Hoes 但并不是不堪入目的视频
Am I the only one gettin… tired? 难道我是唯一一个守旧派?
Why is everybody so obsessed? 为什么人人对此着迷?
Money can't buy us happiness 满足与快乐 金钱无法买到
Can we all slow down and enjoy right now 我们就不能停 活在当下
Guarantee we'll be feelin 我能保证是个好方法
All right 极好的
Everybody look to their left (yeah) 人人向左看看(耶)
Everybody look to their right (ha) 人人向右看看(哈)
Can you feel that (yeah) 难道感觉不到吗(耶)
Well pay them with love tonight… 爱能让你今晚圆满...
It's not about the money, money, money 这无关金钱 金钱 金钱
We don't need your money, money, money 我们也不需要人们的金钱 金钱 金钱
We just wanna make the world dance 我只要世人跟我手舞足蹈
Forget about the Price Tag 共同忘掉身价牌
Ain't about the (uh) Ka-Ching Ka-Ching 不是关于(哼)金钱 金钱
Aint about the (yeah) Ba-Bling Ba-Bling 也不是关于(耶)穿金戴银
Wanna make the world dance 只想让世人共舞
Forget about the Price Tag 共同忘掉身价牌
Yeah yeah 耶耶
well, keep the price tag 好吧 继续生活在标签下
and take the cash back 把现金全部拿回来
just give me six streams and a half stack 只要给我六首音频和500MB(注:stack电脑术语 等于1GB)
and you can keep the cars 你就可以得到我的车
leave me the garage 开出我的车库
and all I..而我呢……
yes all I need are keys and garage 是啊 我只要吉他和音符陪伴我
and guess what, in 30 seconds I'm leaving to Mars 猜想着,我在30秒能飞向火星
yes we leaving across these undefeatable odds 是的,我们将不朽打破并抛离
its like this man, you can't put a price on life 像这传奇人物 你无法给其定价
we do this for the love so we fight and sacrifice everynight 每晚战斗和牺牲 一切都是为了爱
so we aint gon stumble and fall never 所以我们永远不会堕落
waiting to see, a sign of defeat uh uh 看着吧,你们的标志注定失败
so we gon keep everyone moving there feet 所以我们要大家继续前进
so bring back the beat and everybody sing 重复这节奏 大家一起唱
It's not about the money, money, money 这无关金钱 金钱 金钱
We don't need your money, money, money 我们也不需要人们的金钱 金钱 金钱
We just wanna make the world dance 我只要世人跟我手舞足蹈
Forget about the Price Tag 共同忘掉身价牌
Ain't about the (uh) Ka-Ching Ka-Ching 不是关于(哼)金钱 金钱
Aint about the (yeah) Ba-Bling Ba-Bling 也不是关于(耶)穿金戴银
Wanna make the world dance 只想让世人共舞
Forget about the Price Tag 共同忘掉身价牌
It's not about the money, money, money 这无关金钱 金钱 金钱
We don't need your money, money, money 我们也不需要人们的金钱 金钱 金钱
We just wanna make the world dance 我只要世人跟我手舞足蹈
Forget about the Price Tag 共同忘掉身价牌
Ain't about the (uh) Ka-Ching Ka-Ching 不是关于(哼)金钱 金钱
Aint about the (yeah) Ba-Bling Ba-Bling 也不是关于(耶)穿金戴银
Wanna make the world dance 只想让世人共舞
Forget about the Price Tag 共同忘掉身价牌
Yeah yeah 耶耶
oo-oooh 哦哦
Forget about the Price Tag 共同忘掉身价牌'''
print ('money一共出现了'+str(song.count('money')))

将字符串分解成一个个的单词。

string = 'To be or not to be that is the question '
print(string.split())

2.组合数据类型练习

分别定义字符串,列表,元组,字典,集合,并进行遍历。

#字符串遍历
s =  'The man who has made up his mind to win will never say " Impossible".'
for i in s:
    print(i)

#遍历方法2
for i in range(len(s)):
        print(s[i])

#列表遍历
l = ['apple','banana','peal','milk']
for i in l:
    print(i)

#遍历方法2
for i in range(len(l)):
        print(l[i])

#元祖遍历
t = ('apple','banana','peal','milk')
for i in d:
    print(i)    

#字典遍历
d = dict(zip(['k1','k2','k3'],(1,2,3)))
d.items()
dict_items([('k1', 1), ('k2', 2), ('k3', 3)])

#遍历方法2
for i in d:
    print(i+':'+str(d[i]))

#集合遍历
s = set('12344')
for i in s:
    print (i)

总结列表,元组,字典,集合的联系与区别。

1.列表和元祖是有序的(可以通过下标访问),元祖和集合是无序的

2.列表和字典的值是可变的,元祖、集合、字典的键是不可变的

3.列表和元祖(有序)会因为其中元素增多而速度变慢,字典和集合(无序)不会

原文地址:https://www.cnblogs.com/RE148/p/8608295.html