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
#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


Messages In This Thread
Replacing a words' letters in a string - by cananb - Dec-01-2020, 04:56 PM
RE: Replacing a words' letters in a string - by deanhystad - Dec-01-2020, 06:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing String Variable with a new String Name kevv11 2 794 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Pulling Specifics Words/Numbers from String bigpapa 2 776 May-01-2023, 07:22 PM
Last Post: bigpapa
  Extract a string between 2 words from a text file OscarBoots 2 1,885 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,828 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  check if letters in a words Vimart 4 5,528 Aug-10-2020, 09:59 PM
Last Post: deanhystad
  how to check if string contains ALL words from the list? zarize 6 7,262 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,982 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,113 Jan-08-2020, 08:02 AM
Last Post: perfringo
  Replacing characters in a string with a list cjms981 1 1,824 Dec-30-2019, 10:50 PM
Last Post: micseydel
  Select single letters from string based on separate string Jpen10 3 2,681 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