In Python I have a string that looks like this:
str = '1,2,3,4'
I have a method that takes four arguments:
def i_take_four_arguments(a, b, c, d):
return a + b + c + d
What do I have to do to string to allow it to be sent to the method?
In Python I have a string that looks like this:
str = '1,2,3,4'
I have a method that takes four arguments:
def i_take_four_arguments(a, b, c, d):
return a + b + c + d
What do I have to do to string to allow it to be sent to the method?
If you want to perform arithmetic operation, following is one way. The idea is to convert your split string values to integers and then pass them to the function. The *
here unpacks the generator
into 4 values which are taken by a
, b
, c
and d
respectively.
A word of caution: Don't use in-built function names as variables. In your case, it means don't use str
string = '1,2,3,4'
def i_take_four_arguments(a, b, c, d):
return a + b + c + d
i_take_four_arguments(*map(int, string.split(',')))
# 10
str = '1,2,3,4'
list_ints = [int(x) for x in str.split(',')]
This will give you a list of integers, which can be added, pass them to your function