字符串练习

1.字符串练习:

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

取得校园新闻的编号

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

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

产生python文档的网址

tt='https://docs.python.org/3/library/turtle.html'
print(tt)

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

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

for i in range(20):
print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))

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

用函数得到校园新闻编号

com2 = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
com3 = '.html'
for i in range(2,20):
print( com2 + str(i) + com3)

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

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

song = '''
我一生的黄金时代
细看过沉默的大多数
验证本质无能的愤怒
行驶特立独行的路途
我一生的黄金时代
细看过沉默的大多数
验证本质无能的愤怒
行驶特立独行的路途
只等有一天
你说出水中有蜃楼
我就与你拂袖而奔
整个灵魂交付与你
想变成天上忽明忽暗的云朵
想吃掉世上最美味的一切
一想到你呀 我这张脸就泛起微笑
爱你就像爱生命
爱你就像爱生命
当我跨过沉沦的一切
向着永恒开战的时候
你是我不倒的旗帜
爱你就像爱生命
爱你就像爱生命
流年似水经过
阿芙罗蒂从浪花里浮现
淡淡地爱着海流山川
全心全意爱另一座冰山
想变成天上忽明忽暗的云朵
想吃掉世上最美味的一切
一想到你呀 我这张脸就泛起微笑
爱你就像爱生命
爱你就像爱生命
当我跨过沉沦的一切
向着永恒开战的时候
你是我不倒的旗帜
爱你就像爱生命
什么都不是爱的对手
与之相配的 只有爱
别怕一切美好消失
来吧 先让它存在
爱你就像爱生命
爱你就像爱生命
爱你就像爱生命
print('爱你就像爱生命在歌里一共出现了'+ str(song.count('爱你就像爱生命')) + '次')

2.组合数据类型练习

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

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

string = 'Live or Die that is the question '
print(string.split())

s = "(1,2,3)"
for a in range(len(s)):
print(s[a])


list = ['people','human','money']
for b in list:
print(b)

for c in range(len(list)):
print(list[c])


tuple = ('people','human','money')
for d in tuple:
print(d)
for e in range(len(tuple)):
print(tuple[e])


dict = {'people':10,'human':16,'money':28}
for key in dict:
print(dict[key])


dom = set(['people','human','money'])
for f in dom:
print(f)

原文地址:https://www.cnblogs.com/shadows24/p/8619056.html