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


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

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

    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 i in range(1,10):
        for j in range(1,10):
            print("{}x{}={}".format(i,j,i*j),end=' ')
            if i==j:
                print("
    ")
                break
    结果:

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

    s = '''An unlucky fox fell into a well, It was quite deep, so he could not get out by himself. 
    A goat came. He asked the fox:”what are you doing? The fox said : "There will be no water, so I jumped down to get some water. Why don’t you jump down, too?"
    The goat jumped into the well.
    But the fox jumped on the goat’s back and out of the well. "Good-bye, friend," said the fox. "Remember next time don’t trust the advice of a man in difficulties."
    Thank you!'''
    print(s)
    word = input("请输入查询的单词:")
    print("该单词出现的次数为:%d"%str(s).count(word))
    for i in s:
        s=str(s).replace(","," ")
        s=str(s).replace("."," ")
        s=str(s).replace("?"," ")
        s=str(s).replace("!"," ")
        s=str(s).lower()
    print(s)
    结果:

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

    import webbrowser as web
    web.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/')

    结果:


  • 相关阅读:
    必备java参考资源列表
    java中使用js函数
    6.游戏特别离不开脚本(3)-JS脚本操作java(2)(直接解析JS公式,并非完整JS文件或者函数)
    6.游戏特别离不开脚本(3)-JS脚本操作java(直接解析JS公式,并非完整JS文件或者函数)
    6.游戏特别离不开脚本(2)-获取脚本引擎
    6.游戏特别离不开脚本(1)-原因
    5.eclipse 自带的jdk没有源码,改了它
    4.调个绿豆沙护眼色
    3.myeclipse 8.5 m1 注册码
    Zxing中文乱码的简单解决办法
  • 原文地址:https://www.cnblogs.com/001688yzl/p/7544787.html
Copyright © 2020-2023  润新知