字符串操作练习:星座、凯撒密码、99乘法表

  1. 实例:输出12个星座符号,以反斜线分隔。
    for i in range(12):
       print (chr(9800+i),end="/")
  2. 实例:恺撒密码的编码
    plaincode=input ("请输入明文:")
    for p in plaincode:
        if ord("a")<=ord("p")<=ord("z"):
            print(chr(ord("a")+(ord(p)-ord("a")+3)%26),end="")
        else:
            print(p,end="")
  3. 输入姓名,格式输出:占4位、居中、不足4字的以空格填充。
    a = input('name:')
    print('the name what you entered: {0:^4}'.format(a))
  4. 格式化输出:中华人民共和国国内生产总值(GDP)689,136.89亿元(2015年)(千分位、2位小数,浮点数)
    print("中华人民共和国国内生产总值(GDP):{0:^-10,.2f}亿元".format(689136.89))
  5. 实例:打出99乘法表
    for i in range(1,10):
        for j in range(1,i+1):
            print(j,"*",i,"=",i*j," ",end='')
        print(end='\n')

     

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

s='''we wish you a merry christmas 
we wish you a merry christmas 
we wish you a merry christmas 
and a happy new year 
good tidings we bring 
to you and your kin 
good tidings for christmas 
and a happy new year 
we wish you a merry christmas 
we wish you a merry christmas 
we wish you a merry christmas 
and a happy new year 
and have yourself a merry little christmas, 
have yourself a merry little christmas night 
Jingle Bells 
(The One Horse Open Sleigh) 
Lyrics:James Lord Pierpont Music:James Lord Pierpont 

Dashing through the snow, 
In a one horse open sleigh, 
Over the fields we go, 
Laughing all the way; 
Bells on bobtails ring, 
Making spiritis bright, 
What fun it is to ride and sing a sleighing song to night. 

Jingle Bells! Jingle Bells! Jingle all the way! 
Oh, what fun it is to ride in a one horse open sleigh! 
Jingle Bells! Jingle Bells! Jingle all the way! 
Oh, what fun it is to ride in a one horse open sleigh! 

A day or two ago I thought I'd take a ride, 
And soon Miss Fannie Bright, 
Was seated by my side; 
THe horse was lean and lank, 
Misfortune seemed his lot, 
He got into a drifted bank, 
And we, we got upsot. 

Jingle Bells! Jingle Bells! Jingle all the way! 
Oh, what fun it is to ride in a one horse open sleigh! 
Jingle Bells! Jingle Bells! Jingle all the way! 
Oh, what fun it is to ride in a one horse open sleigh! 

Now the ground is white, 
Go it while you're young, 
Take the girls tonight, 
And sing this sleighing song. 
Just get a bobtailed bay, 
Two-forty for this spead, Then hitch him to an open sleigh, 
And crack! You'll take the lead. 

Jingle Bells! Jingle Bells! Jingle all the way! 
Oh, what fun it is to ride in a one horse open sleigh! 
Jingle Bells! Jingle Bells! Jingle all the way! 
Oh, what fun it is to ride in a one horse open sleigh!'''
print('the 出现的次数:',s.count('the'))
s=s.replace(',',' ')
s=s.replace('.',' ')
s=s.replace('?',' ')
s=s.replace('!',' ')
s=s.lower()
print(s)

 

转载于:https://www.cnblogs.com/zsc-leo/p/7543520.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值