我是爱哭鬼  

1、将一串字符串反向输出,如将"abcd"变成“dcba”

1
2
str1="abcd"
print str1[::-1]

2、判断是否为回文——判断用户输入的字符串是否为回文。回文是指正反拼写形式都是一样的词,譬如“racecar”。

1
2
3
4
5
6
7
8
9
while(1):
    input_str = raw_input("please input your string:")
    if input_str=="quit":
        print "bye"
        break
    elif input_str==input_str[::-1]:
        print "your input is huiwen"
    else:
        print "your input is not huiwen"

3、统计一段英文语句的字母个数:

#!coding=utf-8
txt="Failure is probably the fortification in your pole"
count={}
for i in txt:
    count.setdefault(i,0)#如果字典中没有key为i的键,则添加这个键,并设置该键的值为0。如果已经有这个键了,则不更改该字典
    count[i]=count[i]+1
print count

 4、统计一段英文语句中单词的个数:

1
2
3
4
5
6
7
8
#!coding=utf-8
txt="Failure is probably the fortification in your pole is"
strlist=txt.split(" ")
countWord={}
for i in strlist:
    countWord.setdefault(i,0)
    countWord[i]=countWord[i]+1
print countWord

  

posted on   我是爱哭鬼  阅读(406)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
 
点击右上角即可分享
微信分享提示