By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,385 Members | 1,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,385 IT Pros & Developers. It's quick & easy.

Help with replacing all numbers with the letter 'd'

P: 1
this wont work in codewrite when i import 're' is there another way?

Example 'aa1aa2aa3' should change to 'aadaadaad'


Expand|Select|Wrap|Line Numbers
  1. st = raw_input("Enter string: ")
  2.  
  3. def discover_digits(st):
  4.  
  5. ....import re
  6. ....digit = re.sub('\d', 'd', 'aa1aa2aa3') # \d means  \   .........any digit
  7. ....print digit
  8. ....return digit
  9.  
  10. discover_digits(st)
Oct 18 '13 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 626
Expand|Select|Wrap|Line Numbers
  1. orig_string = "aa1aa2aa3"
  2. output_string = ""
  3. for letter in orig_string:
  4.     if letter.isdigit() :
  5.         output_string += "d"
  6.     else:
  7.         output_string += letter
  8. print output_string 
Oct 22 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.