字符串操作练习:星座、凯撒密码、99乘法表、词频统计预处理

1.输出12个星座符号,以反斜线分隔;

for i in range(12):
    print(chr(9800+i),end='/')

2.恺撒密码的编码

kaisa=input('明文:')
s=ord('a')
for i in kaisa:
    if s<= ord(i)<=ord('z'):
        print(chr(s+(ord(i)-s+3)%26),end='')
    else:
        print(i,end='')

3.输入姓名,格式输出:占4位、居中、不足4字的以空格填充。

#{ }里的格式控制 <序号>:<填充><对齐><宽度><千分位><精度><类型>
name=input('输入姓名:')
print('你的名字:{0:' '^4}'.format(name))

4.格式化输出:中华人民共和国国内生产总值(GDP)689,136.89亿元(2015年)(千分位、2位小数,浮点数)

    #format()的{}以0为起点

print('中华人民共和国国内生产总值(GDP):{0:,.2f}亿元({1}年)'.format(689136.89,"2015"))

5.打出99乘法表

for x in range(1,10):
    for y in range(1,x+1):
        print('{}x{}={}'.format(x,y,x*y),end=' ')
    print('\n')

6.下载一首英文的歌词或文章,统计单词出现的次数,将所有,.?!替换为空格,将所有大写转换为小写。

#实例: 下载一首英文的歌词或文章,统计单词出现的次数
#将所有,.?!替换为空格,将所有大写转换为小写。
song='''
Oh woah
Oh woah
Oh woahohhohh
 
You know you love me, I know you care
You shout whenever, and I'll be there
You want my love, you want my heart
And we will never ever ever be apart
Are we an item? Girl quit playing
We're just friends, what are you saying
Said there's another and look right in my eyes
My first love broke my heart for the first time
And I was like
 
Baby, baby, baby, oh like
Baby, baby, baby, no like
Baby, baby, baby, oh
I thought you'd always been mine, mine
Baby, baby, baby, oh like
Baby, baby, baby, no like
Baby, baby, baby, oh
I thought you'd always been mine, mine
Oh, Oh
 
For you, I would have done whatever
And I just can't believe we ain't together
And I wanna play it cool, but I'm losing you
I'll buy you anything, I'll buy you any ring
And I'm in pieces, baby fix me
And you'll shake me till you wake me from this bad dream
I'm going down, down, down, down
And I just can't believe my first love won't be around
And I'm now like
 
Baby, baby, baby, oh like
Baby, baby, baby, no like
Baby, baby, baby, oh
I thought you'd always been mine, mine
Baby, baby, baby, oh like
Baby, baby, baby, no like
Baby, baby, baby, oh
I thought you'd always been mine, mine
 
When I was 13 I had my first love
There was nobody that compared to my baby
And nobody came between us or could ever come above
She had me going crazy
Oh I was starstruck
She woke me up daily don't need no starbucks
She made my heart pound
Asking for a beat when I see her in the street
And at school on the playground
But I really wanna see her on the weekend
She knows she got me dazy
Cause she was so amazing
And now my heart is breaking
But I'll just keep on saying
 
Baby, baby, baby, oh like
Baby, baby, baby, no like
Baby, baby, baby, oh
I thought you'd always been mine, mine
Baby, baby, baby, oh like
Baby, baby, baby, no like
Baby, baby, baby, oh
I thought you'd always been mine, mine
Now I'm all gone
Now I'm all gone
Now I'm all gone
Now I'm all gone
I am gone

'''
print('统计baby出现的次数为'+str(song.count('baby'))+'')#print内只能输出字符串
print('统计Baby出现的次数为'+str(song.count('Baby'))+'')

for i in song:
    song=song.replace(',',' ')#不要忘记写回原文件去,否则一次的修改是会被覆盖的
    song=song.replace('?',' ')
    song=song.replace('!',' ')
    song=song.replace('.',' ')
    
print(song)
print('转换为小写'.center(50,'='))

song=song.lower()
print(song)
print('统计baby出现的次数为'+str(song.count('baby'))+'')

Song

7.用webbrowser,uweb.open_new_tab('url')打开校园新闻列表

import webbrowser as kok_web
kok_web.open_new_tab('www.baidu.com')
for i in range(2,5):
     kok_web.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/'+str(i)+'.html')

 

posted @ 2017-09-18 15:41  ELsky  阅读(211)  评论(0编辑  收藏  举报