简体   繁体   中英

Variable number of arguments in python

def func(*arg):
    for i in arg:
        print(i)

Here if a user is allowed to enter the arguments using input function (python 3.x).

Eg : "Hello,World,Good,Morning"

If we want each word which is seperated by comma must be passed as argument...

Eg: func("Hello",World","Good","Morning")

How can I solve this problem?

Use * during function call too. str.split splits the string into a list and then unpack the list items into individual arguments using * .

>>> strs =  "Hello,World,Good,Morning"
>>> spl = strs.split(',')
>>> spl
['Hello', 'World', 'Good', 'Morning']
>>> func(*spl)
Hello
World
Good
Morning

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM