字符串练习

字符串练习:

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

取得校园新闻的编号

 

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

 

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

产生python文档的网址

 

newshead='https://docs.python.org/3/library/'
newsend='.html'
print(newshead+str('turtle')+newsend)

 

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

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

news1='http://news.gzcc.cn/html/xiaoyuanxinwen/'
news2='.html'
for i in range(10):
    news3=news1+str(i)+news2
    print(news3)

 

 

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

用函数得到校园新闻编号

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

 

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

song='''I've been reading books of old
The legends and the myths
Achilles and his gold
Hercules and his gifts
Spiderman's control
And Batman with his fists

And clearly I don't see myself upon that list
She said where'd you wanna go
How much you wanna risk
I'm not looking for somebody
With some Superhuman gifts
Some Superhero

Some fairytale bliss
Just something I can turn to
Somebody I can kiss
I want something just like this
Doo doo doo doo doo doo

Doo doo doo doo doo

Doo doo doo doo doo doo
Oh I want something just like this
Doo doo doo doo doo doo

Doo doo doo doo doo

Doo doo doo doo doo doo
Oh I want something just like this

I want something just like this

I've been reading books of old

The legends and the myths
The testaments they told
The moon and its eclipse
And Superman unrolls
A suit before he lifts

But I'm not the kind of person that it fits
She said where'd you wanna go
How much you wanna risk
I'm not looking for somebody
With some Superhuman gifts
Some Superhero

Some fairytale bliss
Just something I can turn to
Somebody I can miss
I want something just like this

I want something just like this

Oh I want something just like this
Doo doo doo doo doo doo

Doo doo doo doo doo

Doo doo doo doo doo doo
Oh I want something just like this
Doo doo doo doo doo doo

Doo doo doo doo doo
Doo doo doo doo doo doo

Where'd you wanna go
How much you wanna risk
I'm not looking for somebody
With some Superhuman gifts
Some Superhero
Some fairytale bliss
Just something I can turn to
Somebody I can kiss
I want something just like this

Oh I want something just like this

Oh I want something just like this

Oh I want something just like this
'''
print('some在歌曲里一共出现了'+ str(song.count('some')) +'')

 

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

str='Whered you wanna goHow much you wanna risk'
print(str.split())

 

2.组合数据类型练习

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

字符串:

str='hiro'
for s in str:
    print(s)

列表:

ls=['h','i','r','o']
for s in ls:
    print(s)

 

元组:

tuple=('h','i','r','o')
for s in tuple:
    print(s)

 

字典:

dict = dict(zip('hiro', '5826'))
for s in dict:
    print(s)
for s in dict.keys():
     print(s)
for s in dict.values():
    print(s)
for s in dict.items():
    print(s)

 

集合:

set = (['hiro'],'hjkl')
for i in set:
    print(i)

 

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

联系:

元组可以包含列表,列表衣可以包含元组。

他们能够相互转化。

区别:

元组和列表是有序列的,字典和集合是无序列。

转载于:https://www.cnblogs.com/a565810497/p/8613192.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值