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

Trying to input a fraction and split it into a list but it causes problem when the numbers are more than 1 digit

fraction = list(input("Enter: "))
print(fraction)

When Inputting "99/100" I wanted it to print ["99", "/", "100"] not [‘9’, ‘9’, ‘/’, ‘1’, ‘0’, ‘0’]

>Solution :

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

You can use ".split" for this, for example:

thing1 = "abcdef"
thing1.split("c")

Gives the output:

['ab', 'def']

If you’re bored and fancy fighting some spaghetti code for your own interest, it can be a fun challenge to code something equivalent to ".split()" yourself. Doing so gets you thinking about how Python works and trying to do it in the smallest big-O can introduce you to some interesting stuff about strings, arrays, and algorithm efficiency.

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