Censor


#1



def censor(text,word):
text = text.split
for string in text.split(''):
if string == word:
newtext = len(word) * '*'
else:
newtext = newtext+string
return newtext

Oops, try again.
Your code looks a bit off--it threw a "'builtin_function_or_method'
object has no attribute 'split'" error. Check the Hint if you need help!


How to use 
string.split()
# and 
" ".join(list)


Replace this line with your code.


#2

>>> string = "abcdefghijklmnopqrstuvwxyz"
>>> string.split()
['abcdefghijklmnopqrstuvwxyz']
>>>

Notice that split returns a list. In the above, since we did not specify where to break the stirng, it returns as a single string in a list.

>>> string.split('')
Traceback (most recent call last):
  File "<pyshell#350>", line 1, in <module>
    string.split('')
ValueError: empty separator
>>>

This shows that we cannot use split to create a list from a single string.

>>> list(string)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>>

So it would appear we can only use split when there is a specified separator.

>>> string.split('m')
['abcdefghijkl', 'nopqrstuvwxyz']
>>>

Notice that the m is now gone, but we have two strings in the list.

>>> string = "A quick brown fox jumps over the lazy dog"
>>> string.split()
['A', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
>>>

Above we split a phrase into a list. Notice that there are no space characters? split defaults to space character if separator is not specified. Same as, string.split(' ').


#3

join is both a string and a list method. In strings we can insert a character between every letter...

>>> string = "abcdefghijklmnopqrstuvwxyz"
>>> ' '.join(string)
'a b c d e f g h i j k l m n o p q r s t u v w x y z'
>>>

In lists, we join all list elements into a string with the specified separator...

>>> ' '.join(list(string))
'a b c d e f g h i j k l m n o p q r s t u v w x y z'
>>>

Or,

>>> string = "A quick brown fox jumps over the lazy dog"
>>> string_list = string.split()
>>> string_list
['A', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
>>> '  '.join(string_list)
'A  quick  brown  fox  jumps  over  the  lazy  dog'
>>>

#4

Thank you , its helpful .


#5

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