Skip to content Skip to sidebar Skip to footer

Python — Passing Multiple Arguments

This works in the context of the lesson [Codecademy]. n = ['Michael', 'Lieberman'] # Add your function here def join_strings(lst): concat = '' for string in lst:

Solution 1:

To pass a list to a function:

join_strings(["Michael", "Lieberman"])

Solution 2:

Use asteric operator -

defjoin_strings(*lst):

Your function will interpret the positional parameters as list

Solution 3:

You can pass the list as argument to the function for example in your program you can pass the list like this

n = ["Michael", "Lieberman"]
join_strings(n)

The above code passes list n to join_strings function

To check whether the passed argument is list or not you can have a checking condition like this

defjoin_strings(lst):
    ifisinstance(lst, list):
       #do somethingelse:
       # do something else

in the above code isinstance() method is used to check whether the passed argument is list or not

Solution 4:

I’m sorry guys, apparently I did something wrong when I was typing it into the Terminal or something. Or possibly the way that Sublime (used to) interact with the pre-installed Python stuff on Mavericks. It works fine now, from Sublime and from the Terminal. I’m really not sure what the original problem was or where it was coming from.

— Typing the code in as I had it the first time gave me exactly what I wanted, and then I tweaked it to add a space between the concat strings.

def join_strings(lst):
    concat = ""forstringin lst:
        concat += (string + " ")
    returnconcat

Thanks for all the help, though. Sorry to bother. :)

Post a Comment for "Python — Passing Multiple Arguments"