Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: I'm trying to use math contained in a variable

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading