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


    1、凯撒密码

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

    2、星座符号

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

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

    name=input('输入姓名:')
    print('你的名字:{0:' '^4}'.format(name))

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

     year = input("请输入年份:")
     gdp = float(input("请输入{}年的GDP:".format(year)))
     print("中华人民共和国国内生产总值(GDP){0:,.2f}亿元({1}年)".format(gdp,year))

    6、实例:打出99乘法表

     for i in range(1,10):
         for j in range(1,10):
             print("{}x{}={}".format(i,j,i*j),end=' ')
             if i==j:
                 print("
    ")
                 break

     

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

    geci = '''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 
    geci=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 
    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  '''
    
    
    print(geci.count('love'))
    for i in geci:
        geci=geci.replace(',',' ')
        geci=geci.replace('.',' ')
        geci=geci.replace('!',' ')
        geci=geci.replace('?',' ')
    print(geci)
    print (geci.lower())

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

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

  • 相关阅读:
    leetcode 296 题解(暴力破解)
    2.7最大公约数(递归解法)
    2.6斐波那契数列(多分支递归)
    2.5翻转字符串(递归4 )
    2.4对arr数组元素求和
    2.3 用递归形式打印i到j
    2.2用递归方式解决阶问题
    2.1什么是递归?
    1.用数组的方式实现列表
    如何在电脑上安装Jupyter Notebook
  • 原文地址:https://www.cnblogs.com/millmill/p/7542188.html
Copyright © 2020-2023  润新知