-3

Say if I had a function which takes several integers such as ("22, 3, 2") and ("3, -8, , 3) as parameters.

Is it possible, and how if possible, to individually check and read each value separated by commas.

Bruce219
  • 37
  • 2
  • 11
  • 1
    you're passing them as a string or just as a lot of arguments? – arieljannai Mar 16 '17 at 13:30
  • 1
    Have you tried something already? Please give us something to work with, apart from a question. – PinkFluffyUnicorn Mar 16 '17 at 13:31
  • parse through them by splitting on `,` but what have you tried? This is rather simple and a quick google could yield you many answers – MooingRawr Mar 16 '17 at 13:31
  • What have you tried so far and what problems are you facing? – umayfindurself Mar 16 '17 at 13:31
  • If it's a just a string, use `somestring.split(",")` – Luke Mar 16 '17 at 13:31
  • _"a function which takes several integers such as..."_. `("22, 3, 2")` is not an integer or a collection of integers. It is a single string surrounded by a superfluous pair of parentheses. `("3, -8, , 3)` isn't an integer or a collection of integers. It's not syntactically valid Python at all, since there's no second quote mark. – Kevin Mar 16 '17 at 13:36

2 Answers2

0

if your parameter to function is a string with comma delimiter you can use split function:

my_data = '22, 3, 2'
parsed = my_data.split(',')
print(parsed) # ['22', ' 3', ' 2']

If you would like to have list of tuples you can use:

print(tuple(parsed)) # ('22', ' 3', ' 2')
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
0

You need to use a split, split takes in a "separator" and breaks the string down into an array using the "separator" as an end point:

words = '22, 3, 2'
words2 = words.split(',')

Examples/information: http://www.pythonforbeginners.com/dictionary/python-split

DazstaV3
  • 61
  • 9
  • The quotes used in your example are invalid. Also you are not adding any valuable info to the already existing answer. –  Mar 16 '17 at 13:35
  • Now `word` is an undefined variable. `File "", line 1, in NameError: name 'word' is not defined` –  Mar 16 '17 at 13:37
  • @SembeiNorimaki Thanks for pointing it out – DazstaV3 Mar 16 '17 at 13:39