- #1
zeion
- 466
- 1
Homework Statement
Write the follow function:
def substitute_digits (s, subst):
s is a string and subst is a string of length 10. Return a copy of s, where each digit in s is replaced by the second character of subst, '1' is replaced by the second character of subst, ... , and '9' is replaced by the last character of subst.
For example" The call substitute_digits('Hello 43212', 'xrehTlmnop')
should return the string 'Hello There'.
Homework Equations
The Attempt at a Solution
def substitude_digits(s, subst):
for x in list(s):
if x.isdigit() == True:
s.replace(x, subst[s.index(x)], 1)
return s
s = 'Hello 43212'
subst = 'xrehTlmnop'
print substitude_digits(s, subst)
I can't seen to get the replace method to work.. and also I'm stuck on how to actually replace x with subst[ "the actual digit of x" ] .. is there a way to convert a string to an int?
Thanks.