zoukankan      html  css  js  c++  java
  • 字符串操作练习:星座、凯撒密码、99乘法表

    1. 实例:输出12个星座符号,以反斜线分隔。
      for i in range(9800,9812):
          print(chr(i),end=('\ '))

      结果:

    2. 实例:恺撒密码的编码
      plaincode=input('明文:')
      print('密文:',end='')
      for i in plaincode:
          if 97<=ord(i)<=122:
              print(chr(97+(ord(i)-97+3)%26),end='')
          else:
              print(i,end='') 

      结果:

    3. 输入姓名,格式输出:占4位、居中、不足4字的以空格填充。
      #{ }里的格式控制 <序号>:<填充><对齐><宽度><千分位><精度><类型>
      name = input("请输入你的名字:")
      if len(name)<4:
          print('你的名字是:{0:''^4}'.format(name))
      else:
          print('你的名字是:{0:^}'.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('{}x{}={}'.format(x,y,x*y),end=' ')
          print('
      ')

      结果:

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

      news='''It's normal for fast food brands to poke fun at their competitors,
      but Panera Bread's latest social media campaign isn't cracking jokes.
      It's a call for executives everywhere to put their kiddie meals where their
      mouth is.
      On Wednesday, Panera Bread's founder and CEO, Ron Shaich, launched
      the #KidsMenuChallenge, a social media campaign that challenges other fast food
      executives to spend a week eating the food that's served on their own children's
      menus.'''
      print('新闻:',news)
      
      news=news.lower()#小写
      print('小写:',news)
      
      for i in ',.':#将符号替换成空格
           news=news.replace(i,' ')
      print('符号换成空格:',news)
      
      words=news.split(' ')#用空格分解每一个单词
      
      dic={}#字典
      keys=set(words)#出现的所有单词的集合,字典的key
      
      for i in keys:#记录次数
          dic[i]=words.count(i)
      wc=list(dic.items())#列表
      wc.sort(key=lambda x:x[1],reverse=True)
      print('记录次数:')
      print(wc)

      结果:

      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')

      结果:

  • 相关阅读:
    关闭Pinterest通知
    android——创建camera应用(译)
    Android样式——Styles
    Android Fragment学习(一)
    Win32汇编环境配置
    关于微信检测SDK应用的原理浅析(iOS)
    iOS的Mantle实战
    Objective-C运行时的一些技巧
    Autolayout入门教程
    基于RAC的通用TableView
  • 原文地址:https://www.cnblogs.com/husiqi/p/7544072.html
Copyright © 2011-2022 走看看