Pass string as tuple as arguments python

Question:

If I have a string like:

"spam, foo, moo"

How would I pass that to a function so it would turn into:

myFunction("spam", "moo", "foo")
Asked By: gusg21

||

Answers:

You can split your string to list of things:

your_string = "spam, foo, moo"
your_function(*your_string.split(', '))
Answered By: Yuval Pruss

You can split your string and unpack the elements, in order to get 3 arguments.

With just split, you’d get one argument, a list of 3 strings.

myFunction(*"spam, foo, moo".split(', '))
Answered By: Eric Duminil
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.