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

  1. 实例:输出12个星座符号,以反斜线分隔。
  2. 实例:恺撒密码的编码
    mw=input('请输入明文:')
    print('密文:',end='')
    a=ord('a')
    z=ord('z')
    for i in mw:
        if a<=ord(i)<z:
            print(chr(a+(ord(i)-a+3)%26),end='')
        else:
            print(i,end='')

      运行结果:



  3. 输入姓名,格式输出:占4位、居中、不足4字的以空格填充。
    name=input('输入姓名:')
    print('你的名字:{0:' '^4}'.format(name))
    

      运行结果:



  4. 格式化输出:中华人民共和国国内生产总值(GDP)689,136.89亿元(2015年)(千分位、2位小数,浮点数)
    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('{}×{}={}'.format(x,y,x*y),end=' ')
        print('\n')
    

      运行结果:

  6. 实例: 下载一首英文的歌词或文章,统计单词出现的次数,将所有,.?!替换为空格,将所有大写转换为小写。
    passage='''   Johnson had never been up in an aerophane before and he had read a lot about air accidents, so one day when a friend offered to take him for a ride in his own small phane, Mr. Johnson was very worried about accepting. Finally, however, his friend persuaded him that it was very safe, and Mr. Johnson boarded the plane.
      His friend started the engine and began to taxi onto the runway of the airport. Mr. Johnson had heard that the most dangerous part of a flight were the take-off and the landing, so he was extremely frightened and closed his eyes.
      After a minute or two he opened them again, looked out of the window of the plane, and said to his friend, "Look at those people down there. They look as small as ants, don't they?"
     "Those are ants," answered his friend. "We're still on the ground."'''
    print('短文:',passage)
    
    passage=passage.lower()
    print('\n小写短文:',passage)
    
    print('\n短文the出现的次数:',passage.count('the'))
    
    for i in ',."':
        passage=passage.replace(i,' ')
    print('\n所有符号替换为空格:',passage)
    

           运行结果:

  7. 用webbrowser,uweb.open_new_tab('url')打开校园新闻列表
    import webbrowser as web
    for i in range(2,4):
          web.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/'+str(i)+'.html')
    

          运行结果:

posted @ 2017-09-18 15:55  08邢琼佳  阅读(146)  评论(0编辑  收藏  举报