Skip to content

Instantly share code, notes, and snippets.

@ejmurray
Created July 1, 2015 13:33
Show Gist options
  • Save ejmurray/662218071e44598a532d to your computer and use it in GitHub Desktop.
Save ejmurray/662218071e44598a532d to your computer and use it in GitHub Desktop.
Put the censored text in the sentence.
#!/usr/bin/python
# encoding: utf-8
"""
Created: 01/07/15, 13:11
Description:
Write a function called censor that takes two strings, text and word, as input.
It should return the text with the word you chose replaced with asterisks.
For example: censor("this hack is wack hack", "hack") should return "this **** is wack ****"
Assume your input strings won't contain punctuation or upper case letters.
The number of asterisks you put should correspond to the number of letters in the censored word.
You can use string.split() and " ".join(list) to help you here.
Remember: "*" * 4 equals "****"
After splitting the string with string.split(), you can loop through the indices in the list and replace the words
you are looking for with their asterisk equivalent. Join the list at the end to get your sentence!
"""
def censor(text, word):
empty_text = text.split()
for item in empty_text:
if item == word:
new_word = len(word) * "*"
for item2 in empty_text:
if item2 == word:
new_sentence =
# print new_word
return new_word
a = censor("here is a hacker", "hacker")
print a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment