字符串练习

字符串练习:

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文档的网址

addr1 = 'https://docs.python.org/3/library/'
addr2 = ['turtle', 'index', 'sys']
addr3 = '.html'
for i in addr2:
    print(addr1 + i + addr3)

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

for i in range(2, 231):
    address = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
    print(address)

用函数得到校园新闻编号

addr = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0308/9005.html'
print(addr.rstrip('.html')[-9:])

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

song = '''I never knew
When the clock stopped and I'm looking at you

I never thought I'll miss someone like you

Someone I thought that I knew

I never knew

I should have known something wouldn't be true

Baby you know that I'm so into you

More than I know I should do

So why why why

Why should we waited

And I I I

I should be waiting

Waiting for someone new

Even though that it wasn't you

But I know that it's

Wonderful

Incredible

Baby irrational

I never knew it was obsessional

And I never knew it was with you oooh

Baby if it's just

Wonderful

Incredible

Baby irrational

I never knew it was so sad

Just so sad

I'm so sorry

Even now I just cannot feel you feel me

Hmmm

So why why why

Why should we waited

And I I I

I should be waiting

Waiting for someone new

Even though that it wasn't you

But I know that it's

Wonderful

Incredible

Baby irrational

I never knew it was obsessional

And I never knew it was with you oooh

Baby if it's just

Wonderful

Incredible

Baby irrational

I never knew it was so sad

Just so sad

I'm so sorry

Even now I just cannot feel you fall

I don't even know now

I'm sure you'll wait for me

Even now I just cannot deny

I just hold on so tight

Until you and I never could breathe

Oh

Wonderful

Incredible

Baby irrational

I never knew it was obsessional

And I never knew it was with you until you tell me to

Baby if it's just

Wonderful

Incredible

Baby irrational

I never knew it was so sad

Just so sad

I'm so sorry

Even now I just cannot feel you feel me

'''
print(song.count('Wonderful'))

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

a = 'I love study Python!'
print(a.split())

  

原文地址:https://www.cnblogs.com/FZW1874402927/p/8616706.html