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

  1. 实例:输出12个星座符号,以反斜线分隔。
    for i in range(9800,9812):
        print(chr(i),end='\\')
  2. 实例:恺撒密码的编码
    plaincode=input('请输入明文:')
    print('密文:',end='')
    x=ord('a')
    y=ord('z')
    for i in plaincode:
        if x<= ord(i)<=y:
            print(chr(x+(ord(i)-x+3)%26),end='')
        else:
            print(i,end='')
  3. 输入姓名,格式输出:占4位、居中、不足4字的以空格填充。
    n=input('输入你的名字:')
    print("{0: ^4}".format(n))
  4. 格式化输出:中华人民共和国国内生产总值(GDP)689,136.89亿元(2015年)(千分位、2位小数,浮点数)
    print("中华人民共和国国内生产总值(GDP):{0:^,.2f}亿元".format(689136.89))
  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. 实例: 下载一首英文的歌词或文章,统计单词出现的次数,将所有,.?!替换为空格,将所有大写转换为小写。
    news='''The committee has published two books: The Contemporary and Modern History of Three East Asian Countries in 2005 and A Modern History of East Asia Beyond The Boundaries in 2012.
    Committee members attending a history seminar in Nanjing, east China's Jiangsu province, told Xinhua Sunday that work on a third book has begun and is expected to be completed in 2020.
    Li Xizhu, a fellow of the Institute of Modern History, Chinese Academy of Social Sciences, said scholars from the three countries have reached consensus on the focus of the book.
    "It is to address the differences in how we, the three countries, see history and to respond to the current debate on historical issues," Li said.
    Ueyama Yurika, a Japanese member, said the committee will create contents in line with education practice in each country's context so that the textbooks can be used more widely.
    Scholars agree that a correct perception of history is the foundation for reconciliation in East Asia.
    Japanese scholar Kasahara Tokushi said history textbooks in Japan contain fewer and increasingly more obscure contents on the 1937 Nanjing Massacre.'''
    print(news.count('a'))
    news=news.replace(',',' ')
    news=news.replace('.',' ')
    news=news.replace('?',' ')
    news=news.replace('!',' ')
    news=news.lower()
    print(news)

     

转载于:https://www.cnblogs.com/wlh353/p/7544027.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值