字符串练习

字符串练习:

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

取得校园新闻的编号

 

s='http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html'
str=s[-14:-5]
print(str)

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

产生python文档的网址

 

s1='https//'
s2='docs.'
s3='python.'
s4='org/3/'
s5='library'
s6='/turtle.'
s7='html'
str1=s1+s2+s3+s4+s5+s6+s7
print(str1)

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

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

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

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

用函数得到校园新闻编号

s='http://news.gzcc.cn/html/2017/xiaoyuanxinwen/4.html'
s1=s.lstrip("")
s2=s1.lstrip("http://")
s3=s2.rstrip(".html")[-1:]
print(s3)

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

s='classmate'
s1=s.count('s')
print(s1)

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

s='this is an apple,I like it'
s1=s.split()
print(s1)

2.组合数据类型练习

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

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

字符串 size=‘hello,world’ 

for j in  size:
print(j)

列表size=['big','small','large','bigger'] 

for i in size:
print(i)

元组 tup1=('big','small','large','bigger')

for i in tup1:
print(i)

字典 dict ={'语文':90,'数学':80,'英语':'70'}

for key in dict:
print(key,'分数为:',dict[key])

集合 

 s1={'small','big','large','bigger'}

for i in s1:

  print(i)

列表就像一个清单,在列表中的值可以出现多次,值的数据类型也可以不一样,而且列表的每一个值都有序号,用[]来表示。而元组和列表也是相似的,也有序号,但是元组是只读,只能读,不能修改,用()来表示。字典定义了键与值一对一的关系,它们是以无序的方式进行存储,字典里的值可以是任何数据类型,可以与其它数据类型混用,键只能是唯一的,而值可以不用,字典用{}来表示。集合是一个无序不重复元素集,也是key的集合,但是不存储value。

posted @ 2018-03-21 13:13  161蔡瑞奇  阅读(147)  评论(0编辑  收藏  举报