字符串练习

一、输出十二星座

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

二、明文加密

s = input("请输入,明文密码:")
for i in s:
    if ord("a") <= ord(i) <= ord("z"):
        print(chr(ord("a") + (ord(i) - ord("a") + 3) % 26),end='')
    else:
        print(i,end='')

三、九九乘法表

for i in range(1,10):
    for j in range(1,i+1):
        print("{}*{}={:2} ".format(j,i,i*j),end ='')
    print('')

四、验证字符长度,填充字符

s = input("请输入姓名,四为以内:")
if len(s) == 4:
    print("{}".format(s))
else:
    print("{0:*>4}".format(s))

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

s = '''"You were my everythingthis goes out to someone that was 
once the most important person in my life 
i didn't realize it at the time 
i can't forgive myself for the way i treated you so 
i 
as you would hugged me 
i guess everything you said was a lie 
i think about it, it brings tears to my eyes 
i didn't think you'd wanna see me depressed 
i thought you'd be there for me this i confess 
you said you were my best friend, was that a lie 
now i'm nothing to you you're with another guy 
i tried i tried i tried and i'm trying 
now on the inside it feels like i'm dying 
refrain 
[talking] and i do miss you 
i just thought we were meant to be 
i guess now we'll never know 
the only thing i want is for you to be happy 
whether it be with or without me 
i just want you to be happy " '''
print("you 的个数是:")
print(str(s).count("you"))
print(str(s).replace(","," "),str(s).replace("."," "),str(s).replace("?"," "),str(s).replace("!"," "),str(s).lower())
print(len(s))

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

import webbrowser as web
web.open_new_tab("http://news.gzcc.cn/html/2017/xiaoyuanxinwen_0913/807"+'2'+".html")

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

 

s = input("中华人民共和国国内生产总值(GDP)(2015年)(千分位、2位小数,浮点数)")
i = input("选择格式化样式:(1)千分位;(2)2位小数;(3)浮点型;(4)以上同时处理")
s = float(s)
if i == '1':
    print('{:,}'.format(s))
elif i == '2':
    print('{:.2}'.format(s))
elif i == '3':
    print('{:.2f}'.format(s))
elif i == '4':
    print('{:,.2f}'.format(s))

 

转载于:https://www.cnblogs.com/lfsm/p/7541685.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值