Adding a character between the whitespaces of a multiple word string

Viewed 46

I'm trying to make a function that takes in an undetermined amount of parameters, which are the words, that will add a "+" between the single words. At the end it should still be a string.

def add_words(word1 word2 word3 ...)

output:

"word1+word2+word3 ...")
1 Answers

You can use the .join() method of strings and argument expansion to do this!

def add_words(*words):
    return "+".join(words)
>>> add_words("foo", "bar", "baz")
'foo+bar+baz'

Note that the .join() method is actually being used from the string "+", rather than the argument words

Related