Python学习笔记1209

将string中有元音字母的去掉,replace 函数

1 def anti_vowel(text):
2     for i in text:
3         if i in "aeiouAEIOU":
4             text=text.replace(i,'')
5     return text

输入一个string,例如‘hello’,计算得分

 1 score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, 
 2          "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, 
 3          "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, 
 4          "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, 
 5          "x": 8, "z": 10}
 6          
 7 def scrabble_score(word):
 8     score_point=0
 9     word=word.lower()
10     for i in word:
11         score_point+=score[i]
12     return score_point

text的内容中,有word的部分,替换成**,*的个数跟word的个数有关。这个也可以用replace的函数来写,很简单。

1 def censor(text,word):
2     text=text.split()
3     for i in range(len(text)):
4         if text[i]==word:
5             text[i]='*'*len(word)
6     str1=' '
7     text=str1.join(text)
8 
9     return text

 

posted @ 2014-12-09 22:37  batur  阅读(163)  评论(0)    收藏  举报