字符串练习

字符串练习:

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)

 

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

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

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

 

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

用函数得到校园新闻编号

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

 

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

song = ''' I threw a wish in the well,
Don't ask me, I'll never tell
I looked to you as it fell,
and now you're in my way

I trade my soul for a wish,
pennies and dimes for a kiss
I wasn't looking for this,
but now you're in my way

Your stare was holdin', Ripped jeans, skin was showin'
Hot not, wind was blowin'
Where you think you're going, baby?

Hey, I just met you,
and this is crazy,
but here's my number,
so call me, maybe?

It's hard to look right,
at you baaaabeh,
but here's my number,
so call me, maybe?

Hey, I just met you,
and this is crazy,
but here's my number,
so call me, maybe?

And all the other boys,
try to chaaase me,
but here's my number,
so call me, maybe?

You took your time with the call,
I took no time with the fall
You gave me nothing at all,
but still, you're in my way

I beg, and borrow and steal
Have foresight and it's real
I didn't know I would feel it,
but it's in my way

Your stare was holdin', Ripped jeans, skin was showin'
Hot not, wind was blowin'
Where you think you're going, baby?

Hey, I just met you,
and this is crazy,
but here's my number,
so call me, maybe?

It's hard to look right,
at you baaaabeh,
but here's my number,
so call me, maybe?

Hey, I just met you,
and this is crazy,
but here's my number,
so call me, maybe?

And all the other boys,
try to chaaase me,
but here's my number,
so call me, maybe?

Before you came into my life
I missed you so bad
I missed you so bad
I missed you so, so bad

Before you came into my life
I missed you so bad
And you should know that
I missed you so, so bad

It's hard to look right,
at you baaaabeh,
but here's my number,
so call me, maybe?

Hey, I just met you,
and this is crazy,
but here's my number,
so call me, maybe?

And all the other boys,
try to chaaase me,
but here's my number,
so call me, maybe?

Before you came into my life
I missed you so bad
I missed you so bad
I missed you so so bad

Before you came into my life
I missed you so bad
And you should know that

So call me, maybe? '''
print(song.count('call me, maybe'))

 

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

sentence = 'This is my Python homework!'
print(sentence.split())

 

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

s = 'Hello world'
for i in s:
    print(i)

name = ['Benny','Mao','Lan']
for i in name:
    print(i)

ls = ['Benny', 'Mao', 'Lan']
tumple = tuple(ls)
for i in tumple:
    print(i)

name = ['Benny', 'Mao', 'Lan']
score = ['98', '99', '100']
d = {'Benny': 98, 'Mao': 99, 'Lan': 100}
print(d)

name = set(['Benny','Mao','Lan'])
for i in name:
    print(i)

 

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

联系:

元组、字典、集合都可以通过列表来创建;

列表中可以包含元组,元组中可以包含列表;

元组中的列表元素是可以修改的;

区别:

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

列表的每一项都可以修改,元组中除了列表外都不可以修改,字典不可以修改键但可以修改值,集合不可以修改每一项;

列表和字典以及集合在定义后可以添加元素,元组不可以添加元素;

列表和元组中的元素可以重复,字典和集合不可以

posted @ 2018-03-20 21:25  165邝启彬  阅读(159)  评论(0编辑  收藏  举报