Python Forum
Replacing a words' letters in a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replacing a words' letters in a string
#1
Hi everyone, I am a beginner in Python. This is a challenge that I've found in a forum. This program is censoring the words' (which has given by the user) letters with "*" except the first letter. For example, If the sentence is "Every breath you take every move you make" and the input word is "every", the output should look like this:


2 incidents found.
Censored lyrics:
E**** breath you take e**** move you make
This was my only idea but it did not work.
text=input("Enter your text: ").lower()
word=input("Choose a word from the previously entered text: ").lower()
def censor(text,word):
t=text.split()
n=[]
for i in t:
    if(i==word):
        n.append("*"*len(word))
    else:
        n.append(i)
return " ".join(n)
print censor(text,word)
Any help would be appreciated!
Reply
#2
Need to indent all code inside a function.

Use i.lower() when testing instead of forcing the entire text to be lower case. You also want to retain the first letter of the censored word.
def censor(text,word):
    t = text.split()
    n = []
    for i in t:
        if(i.lower()==word):
            # Retain first letter if I and append "*"
        else:
            n.append(i)
    return " ".join(n)
Do you also want to return a count? Functions can return more than 1 value.
Reply
#3
If censor-word is followed by punctuation (every., every:, every, etc) it will not be censored.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extract a string between 2 words from a text file OscarBoots 2 262 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 544 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  check if letters in a words Vimart 4 1,993 Aug-10-2020, 09:59 PM
Last Post: deanhystad
  how to check if string contains ALL words from the list? zarize 6 2,138 Jul-22-2020, 07:04 PM
Last Post: zarize
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 1,242 Apr-22-2020, 02:39 PM
Last Post: deanhystad
  I want to filter out words with one letter in a string (pig latin translator) po0te 1 950 Jan-08-2020, 08:02 AM
Last Post: perfringo
  Replacing characters in a string with a list cjms981 1 945 Dec-30-2019, 10:50 PM
Last Post: micseydel
  Select single letters from string based on separate string Jpen10 3 1,524 Dec-15-2019, 01:21 PM
Last Post: Jpen10
  How to Continuously Remove Letters from Words ZQ12 1 1,330 Nov-23-2019, 05:31 PM
Last Post: perfringo
  getting first letters of a string Unknown_Relic 1 844 Nov-12-2019, 11:16 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020