字符串练习

一、输出十二星座

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))

posted on 2017-10-18 08:53  25-李泽易  阅读(212)  评论(0编辑  收藏  举报

导航