字符串练习
1. http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html
取得校园新闻的编号
str = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html' print(str[-9:-5])
2. https://docs.python.org/3/library/turtle.html
产生python文档的网址
addr1 = 'https://docs.python.org/3/library/' addr2 = '.html' addr = addr1 + 'turtle' + addr2 print(addr)
3. http://news.gzcc.cn/html/xiaoyuanxinwen/4.html
产生校园新闻的一系列新闻页网址
for i in range(2,10): print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(str(i)));
4. 用函数得到校园新闻编号
str="http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0309/9009.html" print(str.split("_",2)[1].rstrip(".html"))
5.用函数统计一歌词中单词出现的次数
str='Shades Of Purple Well I wonder could it be When I was dreaming about you baby You were dreaming of' print(str.count("I"))
6.将字符串分解成一个个的单词。
str='So tonight I am gonna get him got a rendezvous at seven Faire amour toujours, so funny. Wanna spend all of this money Should I wear a dress and high heels? Should I go out in my blue jeans? He is the boy I met in my dreams. And I tell you girls: He is unbelievable Sisters, only sometimes You can meet the kind of boy who will always give you joy Then grab him, never let him. Take him deep into your world Be aware of their girls. He is so unbelievable Finally it is me, see me happy. Today I have found him He is the boy of my dreams and he is unbelievable ' print(str.split())
7.字符串遍历
s = "龙的传人cgc" for j in s: print(j)
8.集合遍历
a = ['a','b','c','d','e','f'] for i in range(len(a)): print(i,a[i])
9.字典遍历
lanqiudui = {'cgc':'dfq','wxy':'hmk','lmh':'***'} for c in lanqiudui.items(): print(c) for d in lanqiudui.keys(): print(d) for f in lanqiudui.values(): print(f)
10.元组遍历
tuple ={'a','b','c','d','e','f','h'} for whr in tuple: print(whr)
11.列表遍历
list = ['a', 1, 'b', 2, 'c', 3]; print('for each method 1 : '); for data in list: print(data)
12.列表,元组,字典,集合的联系与区别
区别:
列表(list)是一个可变的序列,而元组(tuple)是一个不可变的序列。
元组:用元括弧括起来的一组元素集合。其特点是内容丌可变,即一旦定义其长度和内容都是固定的。
列表:由中括弧括起来的包含一组元素的集合;其特点是长度和内容都可以改变。
字典:Python中的字典和其它询言的字典是一个意思,是对hashmap的实现;
联系:
四者都是Python中很重要的几种类型,非常实用。