+ 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
6 Antworten
+ 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.
+ 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)
+ 3
Or you could search the string and replace certain words... It's up to you...
+ 2
Thanks. I will try this but I to find out the problem with my code
+ 2
Mbengue Mbor
Another option is to use a dictionary
+ 1
So many thanks