字符串练习:
http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html
取得校园新闻的编号
acc="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html" print(acc[-14:-5])
https://docs.python.org/3/library/turtle.html
产生python文档的网址
abb1="http://doc.python.org/3/library/" abb2=".html" abb3="turtle" print(abb1+abb3+abb2)
http://news.gzcc.cn/html/xiaoyuanxinwen/4.html
产生校园新闻的一系列新闻页网址
add1="http://news.gzcc.cn/html/xiaoyuanxinwen" add2=".html" for i in range(2,10): add=add1 + str(i) + add2 print(add)
练习字符串内建函数:strip,lstrip,rstrip,split,count
用函数得到校园新闻编号
a="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html" print(a.rstrip(".html").split("_")[1])
用函数统计一歌词中单词出现的次数
sing="""发过要发的梦 但热烈渐觉冰冻 跌过痛过便会懂 谁在作弄 沉默背后也想发狂 很想找一刹来放纵 斗胆一次如反恐 也试过太冲动 大件事没计轻重 错过悔过仍要冲 冲出困局 形象似义勇军般英勇 但结果有没有停战的沟通 如戴著面具去掩饰倦容 迎著炮火也依然表情轻松 情绪不可激动 还若轻的举重 来化解一切压力似失踪 如放下面具去也许面红 乘著晚风去追寻逍遥星空 除去假的宽容 还真想不通 曾年少的青葱 忘却世间的缚束 谁明白像和你也许重逢 遗憾太多却不能倒流的钟 如琴声的哀号 连结他都哭诉 谁明白我初衷 烦躁也是个梦 但我真的痛""" print(sing.count("的"))
将字符串分解成一个个的单词。
article="""China is a big old country and it has a history of more than five thousand years. We are proud of its culture and part of the culture even influences the world greatly. For example, the thought of Confucius. As Chinese economy develops very fast and plays an important role in the world, more and more foreigners learn mandarin in Confucius institutions. The essence of Chinese traditional culture can be tested by the time. It units the whole nation and no matter what kinds of disaster comes, we will fight together for the country’s future. Today, many young people lose faith in traditional culture and they are crazy for the western culture. They follow the western fashion and think we are the old style. But some day, they will realize they are wrong to abandon traditional culture.""" a1=article.replace(","," ").replace("."," ").replace(" "," ").replace("’"," ").replace("\n","").split(" ") print(a1)
2.组合数据类型练习
分别定义字符串,列表,元组,字典,集合,并进行遍历。
字符串
BL1 = 'Hello World' for x in BL1: print(x)
列表
BL2=['physics', 'chemistry', 1997, 2000,'李四'] for x in BL2: print(x)
元组
BL3=('Jack','Luxi','faker','张三',2017) for x in BL3: print(x)
字典
BL4={'Massage':98,'Lisa':87,'Wanfox':76,'你是真的强':82} for y in BL4.keys(): print(y) for x in BL4.values(): print(x) for z in BL4.items(): print(z)
集合
BL5=set('A3018王五是个心机BOY') for x in BL5: print(x)
总结列表,元组,字典,集合的联系与区别。
列表可以修改,里面可以嵌套元组。元组不可以修改,但是元组里面可以嵌套列表,嵌套的列表可以修改,列表和元组里面字符串的排列是有顺序的。字典里面的键是唯一的,键的值可以为空,但是字典里面
的值可以相同。集合可以定义一大串字符,但是与字典一样,它们的排列是无序的。元组,列表可以充当集合、字典里面的元素,其中列表可以修改,元组不能修改