1、将一串字符串反向输出,如将"abcd"变成“dcba”
str1="abcd" print str1[::-1]
2、判断是否为回文——判断用户输入的字符串是否为回文。回文是指正反拼写形式都是一样的词,譬如“racecar”。
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、统计一段英文语句中单词的个数:
#!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