on-internet
← Community

How can I separate all the numbers from a string as indicated?

Stack Overflow ·

I'm writing code for a bigger program but I'm stuck on the following issue: The input for my program is a string such as s="cycling;time:1,49;distance:2". I want the program to only return the numbers in the provided string. I have tried doing it like this: def getDigits(s): answer = [] for char in s: if char.isdigit(): answer.append(char) return ''.join(answer) print(getDigits(s="cycling;time:1,49;distance:2")) The output prints out 1492, but I want the numbers to be printed separately. I want my program to print out 1,49 separately from 2 because 1,49 is the cycling time while 2 is the dista

Comments

Sign in or sign up to comment and upvote.

No comments yet.