字符串练习

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串练习相关的知识,希望对你有一定的参考价值。

一、输出十二星座

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

技术分享

 

以上是关于字符串练习的主要内容,如果未能解决你的问题,请参考以下文章

csharp Epicor标准练习片段

golang 去练习片段

从搜索文档中查找最小片段的算法?

ktor HTTP API 练习

JQuery02

指针练习:输出Hello