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

本文涵盖Python编程的多个实用技巧,包括星座符号输出、恺撒密码编码、格式化字符串、99乘法表生成、歌词统计及网页浏览。通过具体示例,读者可以学习到如何使用Python解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.输出12个星座符号,以反斜线分隔;

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

2.恺撒密码的编码

kaisa=input('明文:')
s=ord('a')
for i in kaisa:
    if s<= ord(i)<=ord('z'):
        print(chr(s+(ord(i)-s+3)%26),end='')
    else:
        print(i,end='')

3.输入姓名,格式输出:占4位、居中、不足4字的以空格填充。

#{ }里的格式控制 <序号>:<填充><对齐><宽度><千分位><精度><类型>
name=input('输入姓名:')
print('你的名字:{0:' '^4}'.format(name))

4.格式化输出:中华人民共和国国内生产总值(GDP)689,136.89亿元(2015年)(千分位、2位小数,浮点数)

    #format()的{}以0为起点

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('\n')

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

#实例: 下载一首英文的歌词或文章,统计单词出现的次数
#将所有,.?!替换为空格,将所有大写转换为小写。
song='''
Oh woah
Oh woah
Oh woahohhohh
 
You know you love me, I know you care
You shout whenever, and I'll be there
You want my love, you want my heart
And we will never ever ever be apart
Are we an item? Girl quit playing
We're just friends, what are you saying
Said there's another and look right in my eyes
My first love broke my heart for the first time
And I was like
 
Baby, baby, baby, oh like
Baby, baby, baby, no like
Baby, baby, baby, oh
I thought you'd always been mine, mine
Baby, baby, baby, oh like
Baby, baby, baby, no like
Baby, baby, baby, oh
I thought you'd always been mine, mine
Oh, Oh
 
For you, I would have done whatever
And I just can't believe we ain't together
And I wanna play it cool, but I'm losing you
I'll buy you anything, I'll buy you any ring
And I'm in pieces, baby fix me
And you'll shake me till you wake me from this bad dream
I'm going down, down, down, down
And I just can't believe my first love won't be around
And I'm now like
 
Baby, baby, baby, oh like
Baby, baby, baby, no like
Baby, baby, baby, oh
I thought you'd always been mine, mine
Baby, baby, baby, oh like
Baby, baby, baby, no like
Baby, baby, baby, oh
I thought you'd always been mine, mine
 
When I was 13 I had my first love
There was nobody that compared to my baby
And nobody came between us or could ever come above
She had me going crazy
Oh I was starstruck
She woke me up daily don't need no starbucks
She made my heart pound
Asking for a beat when I see her in the street
And at school on the playground
But I really wanna see her on the weekend
She knows she got me dazy
Cause she was so amazing
And now my heart is breaking
But I'll just keep on saying
 
Baby, baby, baby, oh like
Baby, baby, baby, no like
Baby, baby, baby, oh
I thought you'd always been mine, mine
Baby, baby, baby, oh like
Baby, baby, baby, no like
Baby, baby, baby, oh
I thought you'd always been mine, mine
Now I'm all gone
Now I'm all gone
Now I'm all gone
Now I'm all gone
I am gone

'''
print('统计baby出现的次数为'+str(song.count('baby'))+'')#print内只能输出字符串
print('统计Baby出现的次数为'+str(song.count('Baby'))+'')

for i in song:
    song=song.replace(',',' ')#不要忘记写回原文件去,否则一次的修改是会被覆盖的
    song=song.replace('?',' ')
    song=song.replace('!',' ')
    song=song.replace('.',' ')
    
print(song)
print('转换为小写'.center(50,'='))

song=song.lower()
print(song)
print('统计baby出现的次数为'+str(song.count('baby'))+'')

Song

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

import webbrowser as kok_web
kok_web.open_new_tab('www.baidu.com')
for i in range(2,5):
     kok_web.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/'+str(i)+'.html')

 

转载于:https://www.cnblogs.com/ELsky/p/7543387.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值