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
  Replacing String Variable with a new String Name kevv11 2 729 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Pulling Specifics Words/Numbers from String bigpapa 2 723 May-01-2023, 07:22 PM
Last Post: bigpapa
  Extract a string between 2 words from a text file OscarBoots 2 1,827 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 2,757 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  check if letters in a words Vimart 4 5,421 Aug-10-2020, 09:59 PM
Last Post: deanhystad
  how to check if string contains ALL words from the list? zarize 6 7,039 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 2,895 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 2,065 Jan-08-2020, 08:02 AM
Last Post: perfringo
  Replacing characters in a string with a list cjms981 1 1,781 Dec-30-2019, 10:50 PM
Last Post: micseydel
  Select single letters from string based on separate string Jpen10 3 2,630 Dec-15-2019, 01:21 PM
Last Post: Jpen10

Forum Jump:

User Panel Messages

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