1

I am still new to python; so, sorry for the somewhat vague question. I was just curious if it is possible to add more than one input to an argument. For example:

def censored(sentence, word):
    if word in sentence:
        sentence = sentence.replace(word, "*" * len(word))
    return sentence
print censored("foo off", "foo")

This will print out "**** off". Which is what I wanted; but, what if I want to add another input other than "foo".

Is there another way to do that without having to add a third, fourth, and nth argument in the function?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402

5 Answers5

6

Of course you could pass a list, but also you could just use *args. It sometimes depends on how you expect to use the function.

def censored(sentence, *args):
  for word in args:    
    if word in sentence:
        sentence = sentence.replace(word, "*" * len(word))
  return sentence
print censored("foo off", "foo", "bar")

Or as a list or iter

def censored(sentence, words):
  for word in words:    
    if word in sentence:
        sentence = sentence.replace(word, "*" * len(word))
  return sentence
print censored("foo off", ("foo", "bar"))
print censored("foo off", ["foo", "bar"])

Here is a good SO for *args and **kwargs

Community
  • 1
  • 1
doog abides
  • 2,270
  • 16
  • 13
  • Okay the *args is new to me. But it's kinda the same, just different brackets. I get it now. –  Aug 20 '15 at 19:30
  • 1
    There is also **kwargs, you should really get to know them as they are integral to the language. http://stackoverflow.com/questions/3394835/args-and-kwargs – doog abides Aug 20 '15 at 19:31
  • There's a small error in the code: the return statements are inside the for loop. – Doug F Aug 20 '15 at 19:45
3

You can iterate over the bad words and use replace to perform the substitutions if any of the words are present.

def censored(sentence, bad_words):
    for word in bad_words:
        sentence = sentence.replace(word, '*' * len(word))
    return sentence

>>> censored('foo off ya dingus', ['foo', 'dingus'])
'*** off ya ******'
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1

In addition to CoryKramer:

Or create a global variable and use a default value for words.

EXLCUDED_WORDS = ['foo', 'dingus']

def censored(sentence, bad_words=EXCLUDED_WORDS):
    # check if the list is not empty
    if bad_words:
        for word in bad_words:
            sentence = sentence.replace(word, '*' * len(word))
    return sentence

censored('foo off ya dingus')
Maarten
  • 492
  • 7
  • 14
0

you could do it as a list.

def censored(sentence, words):
    for word in words:
        if word in sentence:
            sentence = sentence.replace(word, "*" * len(word))
    return sentence
print censored("foo off", ["foo","asp"])
apnorton
  • 2,430
  • 1
  • 19
  • 34
Timo Kvamme
  • 2,806
  • 1
  • 19
  • 24
0

There are a couple of ways to do this. The easiest is to pass a collection of strings as your second argument. For instance:

def censored(sentence, words):
    for word in words:
        if word in sentence:
            sentence = sentence.replace(word, "*" * len(word))
    return sentence

So, your usage could be:

print("foo that ship", ["foo", "ship"]) # passing a list
print("foo that ship", {"foo", "ship"}) # passing a set

Another way, but one that I wouldn't recommend, is to use variable length argument lists like:

def censored(sentence, *words):
    for word in words:
        if word in sentence:
            sentence = sentence.replace(word, "*" * len(word))
    return sentence

With the usage as:

print("foo that ship", "foo", "ship") # as many arguments as you'd like with the same function definition

The problem with this approach is that you cannot easily extend the list of banned words. However, it is a useful technique to know/understand.

apnorton
  • 2,430
  • 1
  • 19
  • 34
  • No I was making a joke that this poster basically posted the same exact thing as me a few minutes after.. I know he is a he because he is me and I am a he. – doog abides Aug 20 '15 at 19:34
  • 2
    You can pass a list to *args by putting an asterisk in the function call. For example, censored(sentence, *['foo', 'bar']) will work just like censored(sentence, 'foo', 'bar') – Doug F Aug 20 '15 at 19:37
  • 1
    @doogabides I was busy submitting an edit (yet to be peer reviewed) for another answer on this question, as well as testing my code for this answer in IDLE. I did not copy your answer; I only noticed yours after I clicked submit. And, since I agreed with your answer, I upvoted. That's the way SE works--multiple answers that say the same thing, posted in good faith, don't detract from the usefulness of the site. – apnorton Aug 20 '15 at 19:41
  • 1
    @apnorton it's all good. I make wisecracks all day long and it's hard to stop online where it sometimes doesn't translate. There's only so many ways to skin this cat, so the answers are bound to be similar. – doog abides Aug 20 '15 at 19:56