0

How can I make this:

a = '3,3,3,3'
b = a.replace(',','+')
print(b)

Have the output of 12? I want the math inside the variable to be solved.

1 Answers1

1
a = '3,3,3,3'
b = [int(el) for el in a.split(",")]
print(sum(b))

What is going on in the code:

  1. separate input into a list of numbers.
  2. cast strings in that list to ints
  3. sum a list of ints.
matszwecja
  • 6,357
  • 2
  • 10
  • 17