10. Censor - I can't keep all my whitespace



I can't figure out how to keep my whitespace.
I have tried 2 different approaches (see below)
error message:
Your function fails on censor("hey hey hey","hey"). It returns "******" when it should return " *** ***".

def censor(text, word):
    splittet = text.split()
    for each in splittet:
        if each == word:
            stars = len(each) * '*'
            place = splittet.index(word)
            splittet.insert(place, stars)
    return splittet

def censor(text, word):
    splittet = text.split()
    for a,b in enumerate(splittet):
        if b == word:
            stars = len(b) * '*'
            splittet[a] = stars
            splittet[a] = b
    return ''.join(splittet)


BTW: if i use "bad coding" anywhere, please let me know. I don't want bad habits from the beginning.



return ''.join(splittet)

the character between the apostrophes is used to join the list elements together, so if you insert a space between the apostrophes you should be good

also, for your first approach you also need to use join


oh.. thank you very much. it's the small things that matters in coding!

I know, please don't judge :smile:


yep, as for bad practice, you have one here:

stars = len(each) * '*'

you only have to do this once, not every run of the loop


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.