How to remove words and symbols from a string or ignore them? Then how to separate the present numbers in it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to remove words and symbols from a string or ignore them? Then how to separate the present numbers in it?

How to remove words and symbols from a string or ignore them? Then how to separate the present numbers in it? For example: "19 years, 9 months and 8 days." I want to keep just the numbers, then turn every time unit for days, in way that I print my age in days like "7118 days." How to do it?

30th Mar 2021, 8:12 PM
Davi Amâncio
Davi Amâncio - avatar
12 Answers
+ 1
If you want to pick out the integer numbers (and not just every digit), you can try this: https://code.sololearn.com/cte2pk36EPZ6/?ref=app This works for sunny days, but you have to be careful. What about if the number is a float, comes in different order, the numbers are more or less then you’d expected or if some number comes before a punctation mark? This is all case you have to consider if the input comes from an ordinary user. (To calculate the days you need a calendar, because the number of days for a year and a moth is different for various years and months.)
31st Mar 2021, 4:33 AM
Per Bratthammar
Per Bratthammar - avatar
+ 1
Thanks! To do it without import any libs is possible? How?
30th Mar 2021, 10:19 PM
Davi Amâncio
Davi Amâncio - avatar
+ 1
Hi! Well this is an example of course. Only you can specify how you want you program to work. But somehow you have to have some ways to relate the data in the input to your code. When the degree of fredoom increase, codes to handle this type of situation pretty soon becomes complex. Thats why a form for how to input data makes it more easy avoid ambigutieties and errors.
31st Mar 2021, 2:09 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
I really appreciated your effort, thanks a lot man, the point is that it isn't what I what I want but what the question Is asking for... But as you said it was an example, I understand you. Anyways I need a complete answer because I got stucked in this homework and need the answer to study the case, you know, huh?
31st Mar 2021, 2:15 PM
Davi Amâncio
Davi Amâncio - avatar
+ 1
Okey, I understand. But what do you mean with “you attributed a value to the variables”? The data values (numbers) in the input string works with the conditions outpointed in the comments. The numbers are taken from the string and are assigned to the list in the same way the the atricle about the regular expression explained, which I discussed earlier, but without importing the re module. It works! There is no manual assignment there. The only ’manual’ (explicit) assignments to variables are the assignment of the constants (average days in a year and a month) intended for the calculation of the total number of days, but this is somthing completely different. How do you want the program to work? Take the contditions in the comments as an starting point for your explaination: • order on data in the input string; • spaces around the data etc. And: how would you know if a number in the input string refer to number of years, month and days, if you are alloweded to input the values in any order?
31st Mar 2021, 3:01 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
ok, I got it now, I misunderstood but now I got! Thanks for the help!
3rd Apr 2021, 11:32 PM
Davi Amâncio
Davi Amâncio - avatar
+ 1
Man Per Bratthammar , you are so, so, so smart, professional hacker hahaha, for how long have you been programming in python?
4th Apr 2021, 12:21 AM
Davi Amâncio
Davi Amâncio - avatar
+ 1
😁Hi, just take a one step at a time, and you will do great! /Regards Per B
4th Apr 2021, 1:19 PM
Per Bratthammar
Per Bratthammar - avatar
0
Per Bratthammar , you attributed a value to the variables, It must apply for any data the person put in the input, in other words, the age musn't be attributed to a pre-existing variable.
31st Mar 2021, 1:47 PM
Davi Amâncio
Davi Amâncio - avatar
0
How long to get in this level? Hahahah 😂
4th Apr 2021, 1:21 PM
Davi Amâncio
Davi Amâncio - avatar
0
I don’t know how to count... I started my interest for programming a long time ago in the 80s (with BASIC and DOS 🙂). But in long periods I have done completely other things. When it comes to Python, maybe two, three years. But it’s just a hobby to me... I work in another field/area.
4th Apr 2021, 1:42 PM
Per Bratthammar
Per Bratthammar - avatar