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

1.实例:输出12个星座符号,以反斜线分隔。

for i in range(12):
    print(chr(9800+i), end="\\")

 

2.实例:恺撒密码的编码

复制代码
s=input("请输入明文:")
t=ord('a')
print("密文是:",end='')
for i in s:
      if t<=ord(i)<=ord('z'):
            print(chr(t+(ord(i)-t+3)%26),end=' ')
      else:
            print(i,end=' ')
            
复制代码

 

 

 

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

s=input("输入姓名:")
print("输出姓名:{0: ^4}".format(s))

 

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

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

 

 

5.打出99乘法表

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

 

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

复制代码
love='''An empty street An empty house 
A hole inside heart 
I'm all alone and the rooms 
Are getting smaller 
I wonder how I wonder why 
I wonder where they are 
The days we had 
The songs we sang together 
And oh! my love 
I'm holding on forever 
Reaching for a love 
That seems so far 
So I say a litter prayer 
No my dream will take me there 
Where the skies are blue to see you 

My love 
And hope my dream will take me there 
Where the skies are blue to see you 
once again my love 
Overseas from coast to coast 
Find a place I love the most 
Where the fields are green 
To see you once again 
My love '''
print('My Love'.center(60,'-'))
print(love.count('love'))
print(love.replace(',',' '))
复制代码

 

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

复制代码
import webbrowser as web
web.open_new_tab("www.baidu.com")
for i in range(1,3):
    web.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/'+str(i)+".html")
复制代码

 

posted @ 2017-09-18 19:05  45hjq  阅读(137)  评论(0编辑  收藏  举报