Program to replace a word with asterisks in a sentence | Function takes two parameter ; Break down sentence by ' ▁ ' spaces and store each individual word in a different list ; A new string to store the result ; Creating the censor which is an asterisks " * " text of the length of censor word ; count variable to access our word_list ; Iterating through our list of extracted words ; changing the censored word to created asterisks censor ; join the words ; Driver code
def censor ( text , word ) : NEW_LINE INDENT word_list = text . split ( ) NEW_LINE result = ' ' NEW_LINE stars = ' * ' * len ( word ) NEW_LINE count = 0 NEW_LINE index = 0 ; NEW_LINE for i in word_list : NEW_LINE INDENT if i == word : NEW_LINE INDENT word_list [ index ] = stars NEW_LINE DEDENT index += 1 NEW_LINE DEDENT result = ' ▁ ' . join ( word_list ) NEW_LINE return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT extract = " GeeksforGeeks ▁ is ▁ a ▁ computer ▁ science ▁ portal ▁ for ▁ geeks . \ STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL I ▁ am ▁ pursuing ▁ my ▁ major ▁ in ▁ computer ▁ science . ▁ " NEW_LINE cen = " computer " NEW_LINE print ( censor ( extract , cen ) ) NEW_LINE DEDENT