Python Program To Replace Number With String

“How to replace the number with the string” is a common question in python programming which is used in normal programming also. To implement this concept we have to use dictionary data type in python which will give us the replacement string of a number.

01. Example

string = 'If you decode Zeroones, it is combination of multiple of 0 and 1.'
dictionary = {'0':'zero','1':'one','2':'two','3':'three','4':'four', '5':'five','6':'six','7':'seven','8':'eight','9':'nine'}

print("The Original String:n", string)

newString = ''
for element in string:
    if (element.isdigit() == True):
        tempChar = dictionary[element]
        newString = newString + tempChar
    else:
        newString = newString + element
        
print ("nOutput String:n",newString)  

Output:

The Original String:
If you decode Zeroones, it is combination of multiple of 0 and 1.

Output String:
If you decode Zeroones, it is combination of multiple of zero and one.

 

Leave a comment