How can I use split( ) to make censor?

Question

How can I use split( ) to make censor?

Answer

Just like join() uses the string it’s attached to to determine how to join together the string you provide in parentheses, split() does the same for splitting a string passed to it.
For example, if we have my_words_split = " ".split("first second third last"), the resulting list stored in my_words_split will be ["first", "second", "third", "last"].
We’ve done lots of work with lists, so the rest of this function is up to you, but remember to " ".join() your words back together at the end if you take this route!

7 Likes

A post was split to a new topic: Censor replacing using index

2 posts were split to a new topic: Censor

count = 0
for i in words:
    if i == word:
        words[count] = stars
    count += 1
result =' '.join(words)

Someone explain the count variable, how it works? Please.

4 Likes

count is a variable to keep track of the index, so we can use it to update elements in words list

i would have named the variable index, not count.

5 Likes

Yep! I understand …

1 Like

A post was split to a new topic: Function prints correct message but not working

4 posts were split to a new topic: Censor

A post was split to a new topic: Censor

Hi,

This is a bit confusing…
When I try to split my text with the method shown above, it results in: [" “].
So I googled it and tried the method suggested there: text.split(” ").
This worked great. The join function works as described here.

Can you explain this behavior?
Thx

2 Likes

the FAQ just got how split works wrong.

5 Likes

Your count variable corresponds to the index of each item (in the list), & the loop is traversing through each index

1 Like
def censor(text, word):
    words = text.split()
    result = ''
    stars = '*' * len(word)
    count = 0
    for i in words:
      if i == word:
#Why not words[i] = stars?  It would be one conditional, and i would be 
#equal to the index of word at the time that the conditional triggers.  
#Can someone explain why count is needed?
        words[count] = stars
      count += 1
#result joins a space to words, but result wasn't modified in the 
#conditional or for loop, so I don't get this part
    result =' '.join(words)
    return result
		
print censor("this hack is wack hack", "hack")

Although it works, the solution’s reference to joining of result doesn’t make sense to me.
See comments in code for more info.

Also, I was thinking “words[i] = stars” would make more sense since i would end up at the index where it discovered the word.

4 Likes

you could do that, but then use range in the loop, or use a while loop and count. The only reason not to, would be to save list lookups

2 Likes

As @stetim94 pointed out, we need to use range() to generate a list of indices. List index values must be integer.

2 Likes

I fail to understand two things about the answer provided in the course.

What is the point of line 3, where it says "result = ’ ’ ". When I delete it, the function still works fine.

Also, what is the parentheses used for with the split method? i.e. text.split() When I put any string in the parentheses, the text is put in a list, but it is not being split anymore.

1 Like

str.split() is a string method, and like any function cannot be invoked without the parens. If nothing is written in the parens (the argument) then the default argument is ' ' (a space character) so that it splits out the words without whitespace.

The argument is called the separator string and specifies what characters to target as the split point.

"The rain in Spain falls mainly in the plains".split()

gives,

['The', 'rain', 'in', 'Spain', 'falls', 'mainly', 'in', 'the', 'plains']

As we can see, str.split() always returns a list object.

Assuming the quotes indicate an empty string, it would be written without a space.

result = ''

This initializes result as a string object, to which other string characters can be concatenated.

3 Likes

What I specifically don’t understand about the .split method is why, when I use any other string as the separator string, the sentence is not actually being split. It’s just being put into the list as one item.

As for result = ’ ', I don’t really understand why they’ve initialized it as a string object in line 3, if the function works the same without line 3. Is it just good practice to do that for all variables that will appear as part of the function?

1 Like

Without seeing your code can we assume you are growing a string?

Remove the line, save and refresh the page then try to run it again. You should get an error on the line,

result += "word"

NameError: name ‘result’ is not defined

If you are modifying the words list in place by index, then the result will be a string joined from the list.

result = ' '.join(words)

There will be a space character as the separator to restore a sentence.

2 Likes

If you specify a delimiter that does not exist in your string then it’ll be split into one parts. This should not be surprising!

If you create a variable and never read from it, then no, it is not needed. It’s not good practice, no.

2 Likes