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

  1. 实例:输出12个星座符号,以反斜线分隔。
    for i in range(12):
        print(chr(9800+i),end='\\')
  2. 实例:恺撒密码的编码
    plaincode = input('明文:')
    print('密文:',end='')
    for i  in plaincode:
        print(chr(ord(i)+3),end='')
  3. 输入姓名,格式输出:占4位、居中、不足4字的以空格填充。
    a=input("姓名:")
    print("你输入的名字是:{0:^4}".format(a))
  4. 格式化输出:中华人民共和国国内生产总值(GDP)689,136.89亿元(2015年)(千分位、2位小数,浮点数)
    print("中华人民共和国国内生产总值(GDP):{0:^-10,.3f}亿元".format(689,136.89))
  5. 实例:打出99乘法表
    for i in range(1,10):
        for j in range(1,10):
             print("%d*%d=%2d" % (i,j,i*j),end=" ")
        print("")
  6. 实例: 下载一首英文的歌词或文章,统计单词出现的次数,将所有,.?!替换为空格,将所有大写转换为小写。
    song='''
    Someone Like You - Adele
    I heard that you settled down
    我听说你已心有所属
    That you found a girl and your married now
    你找到了一个合适的女孩并和她结了婚
    I heard that your dreams came true
    我听说你已梦想成真
    Guess she gave you things I didn't give to you
    我猜,她给了你我所未能给予的
    Old friend why are you so shy
    老朋友,你害什么羞呢
    It ain't like you to hold back or hide from the lie
    不像你啊,遮遮掩掩的
    I hate to turn up out of the blue uninvited
    我恨她代替了原本属于我的位置
    But I couldn't stay away I couldn't fight it
    但是我没办法离开,我无法释怀
    I'd hoped you'd see my face & that you'd be reminded
    我曾多么希望你再看看我并提醒你
    That for me it isn't over
    并提醒你,对我来说,一切都还未结束
    Nevermind I'll find someone like you
    无所谓,我会找到一个人像你一样
    I wish nothing but the best for you too
    我别无所求,只希望你能过得好
    Don't forget me I beg I remember you said -
    求求你,不要忘记我我记得你说过
    Sometimes it lasts in love but sometimes it hurts instead
    有时爱情可以很永久但有时也会如此伤人
    Sometimes it lasts in love but sometimes it hurts instead yeah
    有时爱情可以很永久但有时也会如此伤人 是啊
    You'd know how the time flies
    你知道时光飞逝
    Only yesterday was the time of our lives
    仿佛我们还在一起
    We were born and raised in a summery haze
    我的爱在夏日的烟雾中萌芽
    Bound by the surprise of our glory days
    充满着惊喜和喜悦
    I hate to turn up out of the blue uninvited
    我恨她代替了原本属于我的位置
    But I couldn't stay away I couldn't fight it
    但是我没办法离开,我无法释怀
    I'd hoped you'd see my face & that you'd be reminded
    我曾多么希望你再看看我并提醒你
    That for me it isn't over yet
    对我来说,一切都还未结束
    Nevermind I'll find someone like you
    无所谓,我会找到一个人像你一样
    I wish nothing but the best for you too
    我别无所求,只希望你能过得好
    Don't forget me I beg I remember you said -
    求求你,不要忘记我我记得你说过
    Sometimes it lasts in love but sometimes it hurts instead
    有时爱情可以很永久但有时也会如此伤人
    Nothing compares no worries or cares
    没什么可与它相比不必担心 不必牵挂
    Regret's and mistakes they're memories made
    我们曾经的爱情里充满了遗憾和误解
    Who would have known how bittersweet this would taste
    又有谁能体会着中间的苦与甜
    Nevermind I'll find someone like you
    无所谓,我会找到一个人像你一样
    I wish nothing but the best for you too
    我别无所求,只希望你能过得好
    Don't forget me I beg I remembered you said -
    求求你,不要忘记我我记得你说过
    Sometimes it lasts in love but sometimes it hurts instead
    有时爱情可以很永久但有时也会如此伤人
    Nevermind I'll find someone like you
    无所谓,我会找到一个人像你一样
    I wish nothing but the best for you too
    我别无所求,只希望你能过得好
    Don't forget me I beg I remembered you said -
    求求你,不要忘记我我记得你说过
    Sometimes it lasts in love but sometimes it hurts instead
    有时爱情可以很永久但有时也会如此伤人
    Sometimes it lasts in love but sometimes it hurts instead yeah yeah
    有时爱情可以很永久但有时也会如此伤人
    -
    '''
    print(song.count("someone"))
    print(song.replace(',',' '))
    print(song.replace('.',' '))
    print(song.replace('?',' '))
    print(song.replace('!',' '))
    print(song.lower())
  7. 用webbrowser,uweb.open_new_tab('url')打开校园新闻列表
    import webbrowser as web
    for i in range(2,6):
        web.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/'+str(i)+'.html')

     

posted @ 2017-09-18 11:35  21黄玺恒  阅读(126)  评论(0编辑  收藏  举报