字符串练习

字符串练习:

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

取得校园新闻的编号

 

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

 



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

产生python文档的网址

 

s1='http://news.gzcc.cn/html/xiaoyuanxinwen/4'
s2='.html'
s=s1+s2
print(s)

 



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

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

 


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

 



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

用函数得到校园新闻编号

s="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html"
print(s.rstrip('.html')[-9:])

 



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

song='''
Trouble will find you no matter where you go, oh oh

No matter if you;re fast, no matter if you;re slow, oh oh

The eye of the storm wanna cry in the morn, oh oh

You;re fine for a while but you start to lose control

He;s there in the dark, he;s there in my heart

He waits in the wings, he;s gotta play a part

Trouble is a friend, yeah trouble is a friend of mine

Ahh..

Trouble is a friend, but trouble is a foe, oh oh

And no matter what I feed him he always seems to grow, oh oh

He sees what I see and he knows what I know, oh oh

So don;t forget as you ease on down my road

He;s there in the dark, he;s there in my heart

He waits in the wings, he;s gotta play a part

Trouble is a friend, yeah trouble is a friend of mine

So don;t be alarmed if he takes you by the arm

I roll down the window, I;m a sucker for his charm

Trouble is a friend, yeah trouble is a friend of mine

Ahh..

How I hate the way he makes me feel

And how I try to make him leave

I try, oh oh I try

He;s there in the dark, he;s there in my heart

He waits in the wings, he;s gotta play a part

Trouble is a friend, yeah trouble is a friend of mine

So don;t be alarmed if he takes you by the arm

I roll down the window, I;m a sucker for his charm

Trouble is a friend, yeah trouble is a friend of mine
'''
print(song.count('Trouble'))

 



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

s='Trouble is a friend'
print(s.split())

 

 

运行截图:

 

 

 

 

 

 

 

posted @ 2018-03-21 15:24  144+粱肇森  阅读(79)  评论(0编辑  收藏  举报