zoukankan      html  css  js  c++  java
  • 字符串操作练习:星座、凯撒密码、99乘法表、词频统计预处理

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

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

    2、实例:恺撒密码的编码。

    r=input('请输入明文:')
    print('输出明文:',end=' ')
    for i in r:
        if ord('a')<=ord(i)<=ord('z'):
            print(chr(ord('a')+(ord(i)-ord('a')+3)%26),end=' ')
        else:
            print(i,end= ' ')

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

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

    s=float(input('请输入GDP:'))
    print('2015年国内GDP为{0:,.2f}亿元'.format(s))

    5、实例:打出99乘法表。

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

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

    >>> cs='''Lately, I’ve been, I’ve been losing sleep
     Dreaming about the things that we could be
     But baby, I’ve been, I’ve been praying hard
     Said, no more counting dollars
     We’ll be counting stars
     Yeah we’ll be counting stars
    
     I see this life like a swinging vine
     Swing my heart across the line
     And my face is flashing signs
     Seek it out and you shall find
     Old, but I’m not that old
     Young, but I’m not that bold
     I don’t think the world is sold
     I’m just doing what we’re told
    
     I feel something so right
     Doing the wrong thing
     I feel something so wrong
     Doing the right thing
     I could lie, could lie, could lie
     Everything that kills me makes me feel alive
    
     Lately, I’ve been, I’ve been losing sleep
     Dreaming about the things that we could be
     But baby, I’ve been, I’ve been praying hard
     Said, no more counting dollars
     We’ll be counting stars
    
     Lately, I’ve been, I’ve been losing sleep
     Dreaming about the things that we could be
     But baby, I’ve been, I’ve been praying hard
     Said, no more counting dollars
     We’ll be, we’ll be counting stars
    
     I feel the love and I feel it burn
     Down this river, every turn
     Hope is a four-letter word
     Make that money, watch it burn
     Old, but I’m not that old
     Young, but I’m not that bold
     I don’t think the world is sold
     I’m just doing what we’re told
    
     I feel something so wrong
     Doing the right thing
     I could lie, could lie, could lie
     Everything that drowns me makes me wanna fly
    
     Lately, I’ve been, I’ve been losing sleep
     Dreaming about the things that we could be
     But baby, I’ve been, I’ve been praying hard
     Said, no more counting dollars
     We’ll be counting stars
    
     Lately, I’ve been, I’ve been losing sleep
     Dreaming about the things that we could be
     But baby, I’ve been, I’ve been praying hard
     Said, no more counting dollars
     We’ll be, we’ll be counting stars
    
     Take that money, watch it burn
     Sink in the river, the lessons are learnt
     Take that money, watch it burn
     Sink in the river, the lessons are learnt
     Take that money, watch it burn
     Sink in the river, the lessons are learnt
     Take that money, watch it burn
     Sink in the river, the lessons are learnt
     Everything that kills me makes feel alive
    
     Lately, I’ve been, I’ve been losing sleep
     Dreaming about the things that we could be
     But baby, I’ve been, I’ve been praying hard
     Said, no more counting dollars
     We’ll be counting stars
    
     Lately, I’ve been, I’ve been losing sleep
     Dreaming about the things that we could be
     But baby, I’ve been, I’ve been praying hard
     Said, no more counting dollars
     We’ll be, we’ll be, counting stars
    
     Take that money, watch it burn
     Sink in the river, the lessons are learnt
     Take that money, watch it burn
     Sink in the river, the lessons are learnt
     Take that money, watch it burn
     Sink in the river, the lessons are learnt
     Take that money, watch it burn
     Sink in the river, the lessons are learnt '''

    统计money出现的次数:

    将,替换为空格:

    将所有大写转换为小写:

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

    >>> import webbrowser
    >>> webbrowser.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/ ')
    True
    >>> 

  • 相关阅读:
    Monkey Studio IDE | The way IDEs should be
    ImportError: No module named pysqlite2 chinacloud 博客园
    EF架构——code first开发中,在修改实体时,自动影响到数据表上
    你必须要知道的架构知识~目录
    MVC中业务层是否应该有个基类?它有什么作用?
    解决COOKIES存储中文乱码的问题
    C#代码是更具艺术性的,选择她,因为喜欢她
    arm驱动程序——按键程序6_互斥1—原子操作(韦东山的视频总结及针对linux2.6.30)
    Oracle体系结构及备份(十)——sgaothers_pool
    Linux进程间通信(三)管道通信之有名管道及其基础实验
  • 原文地址:https://www.cnblogs.com/chenyuanzhao/p/7542164.html
Copyright © 2011-2022 走看看