
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How i can replace number with string using Python?
For this purpose let us use a dictionary object having digit as key and its word representation as value −
dct={'0':'zero','1':'one','2':'two','3':'three','4':'four', '5':'five','6':'six','7':'seven','8':'eight','9':'nine'
Initializa a new string object
newstr=''
Using a for loop traverse each character ch from input string at check if it is a digit with the help of isdigit() function.
If it is digit, use it as key and find corresponding value from dictionary and append it to newstr. If not append the character ch itself to newstr. Complete code is as follows:
string='I have 3 Networking books, 0 Database books, and 8 Programming books.' dct={'0':'zero','1':'one','2':'two','3':'three','4':'four', '5':'five','6':'six','7':'seven','8':'eight','9':'nine'} newstr='' for ch in string: if ch.isdigit()==True: dw=dct[ch] newstr=newstr+dw else: newstr=newstr+ch print (newstr)
The output is as desired
I have three Networking books, zero Database books, and eight Programming books.
- Related Articles
- How can I replace newlines with spaces in JavaScript?
- How can I test if a string starts with a capital letter using Python?
- How can I fill out a Python string with spaces?
- Can we replace a number with a String in a MySQL result set?
- How can I replace & with an ampersand in my MySQL database?
- How to replace all occurrences of a string with another string in Python?
- MySQL: How can I find a value with special character and replace with NULL?
- How to replace string using JavaScript RegExp?
- How can I use MySQL replace() to replace strings in multiple records?
- How can I convert bytes to a Python string?
- How can I convert a Python tuple to string?
- How can I make money with Python?
- How can I filter string value with starting letter?
- Replace a string using StringBuilder
- How to replace Digits into String using Java?

Advertisements