Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

python - Adding a character between the whitespaces of a multiple word string

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 ...")
question from:https://stackoverflow.com/questions/65848589/adding-a-character-between-the-whitespaces-of-a-multiple-word-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...