字符串、组合数据类型练习

1.字符串练习:

1.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]);

1.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);

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

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

for i in range(1,6):
    print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(str(i)));

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

用函数得到校园新闻编号

str="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1211/8772.html"
print(str.split("_",2)[1].rstrip(".html"))

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

str='''Wake up in the morning feeling like P Diddy
Grab my glasses, out the door ,I'm gonna hit this city
Before I leave, brush my teeth with a bottle of Jack
Cause when I leave for the night, I aint coming back
I'm talkin' pedicure on our toes
Trying on all our clothes
Boys blowing up our phones
Drop-topping, playing our favorite CDs
Pulling up to the parties
Trying to get a little bit tipsy
Oh~
Don't stop, make it pop
DJ, blow my speakers up
Tonight, Imma fight
Till we see the sunlight
Tik tok, on the clock
But the party don't stop, no
Woah-oh oh oh
Woah-oh oh oh oh~~ oh~~~~
Aint got a care in world, but got plenty of beer
Aint got no money in my pocket
But I'm already here
And now, the dudes are lining up cause they hear we got swagger
But we kick em to the curb unless they look like Mick Jagger
Im talking about  everybody getting crunk
Boys tryna touch my junk
Gonna smack him if he getting too drunk, drunk
Now, now-we goin til they kick us out
Or the police shut us down,
Police shut us down, down
Po-po shut us down
Don't stop, make it pop
DJ, blow my speakers up
Tonight, Imma fight
Till we see the sunlight
Tik tok, on the clock
But the party don't stop, no
Woah-oh oh oh
Woah-oh oh oh
You build me up
You break me down
My heart, it pounds
Yeah, you got me
With my hands up
You got me now
You got that sound
Yeah, you got me
You build me up
You break me down'''


print(str.count("You"))

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

str="I belive I can fly"
print(str.split())

2.组合数据类型练习

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

字符串:

str="Python is the best computer language"
for i in range(len(str)):
    print("序号: ",i+1,str[i])

列表:

list=['Python','best']
for i in list:
    print("序号:%s   值:%s" % (list.index(i) + 1, i));

元组:

tuple = ('Python','best','language')
for i, val in enumerate(tuple):
       print ("序号:%s   值:%s" % (i + 1, tuple[i]))

字典:

dict = {'Python':6,'best':13,'language':35}
for key in dict:
    print(dict[key])

集合:

dom = set(['Python','best','language'])
for k in dom:
    print(k)

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

  列表购物清单,可以重复,类型可以多种。多数使用“[]”表示。

  元组结构上与列表相似,但只支持只读,不支持修改。多数使用"()"

  字典类似于电话簿,设定的键与值必须是唯一的,如果有重复的键或值,则无法查找到所查找的信息。

  集合就是数学的集合,是一个无序互异的序列。

  都可以互相转换

原文地址:https://www.cnblogs.com/zxc109525/p/8619564.html