How to replace numerals by their corresponding ex: "1" by "one" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to replace numerals by their corresponding ex: "1" by "one"

I have to replace numerals by their corresponding ex: 1 by "one". I've the same output but the code still not valid https://www.sololearn.com/coach/64?ref=app

30th Sep 2021, 4:33 AM
Mbengue Mbor
Mbengue Mbor - avatar
6 Answers
+ 3
Here is the a simple way to do so: 1. Make a list of all string numerals list = ["one", "two", etc....] And then check each character in your string by isdigit() If it is digit just replace it with list[n] Where n is a digit in your string.
30th Sep 2021, 4:41 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 3
And actually it's better to look at word rather than character, because it will fail if you will have something like 11 in there. So your code should be something like: str = input() str = str.split() newstr = "" list = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"] for i in range(len(str)): if str[i].isnumeric(): newstr += list[int(str[i])] + " " else: newstr += str[i] + " " print(newstr)
30th Sep 2021, 5:01 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 3
Or you could search the string and replace certain words... It's up to you...
30th Sep 2021, 5:02 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
Thanks. I will try this but I to find out the problem with my code
30th Sep 2021, 4:44 AM
Mbengue Mbor
Mbengue Mbor - avatar
+ 2
Mbengue Mbor Another option is to use a dictionary
30th Sep 2021, 5:31 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
So many thanks
30th Sep 2021, 5:05 AM
Mbengue Mbor
Mbengue Mbor - avatar