How to Replace Digits in a String with Characters from Another String in Python?

  • Context: Python 
  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    Beginner Python
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 3K views
zeion
Messages
455
Reaction score
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.
 
Physics news on Phys.org
zeion said:


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.


I don't quite understand the point of what you're doing. To convert a string to int, you just have to use

Code:
integer = int(string)
 
Okay I revised it a bit:

def substitude_digits(s, subst):

for x in s:
if x.isdigit() == True:

s.replace(x, subst[int(x)], 1)

return s

s = 'Hello 43212'
subst = 'xrehTlmnop'
print substitude_digits(s, subst)

But the replace still doesn't replace anything.
 
Replace works as follows.
Code:
x = "hello replacement"
y = "chicken"
z = "hello"
k= x.replace(z,y)

Now when you print k, you will get "chicken replacement". So what has happened is this:
We've started with x("hello replacement"). and x.replace(z,y) will replace any occurrence of z("hello") with y("chicken"). So basically it replaces hello with chicken.
 
Last edited:
dacruick said:
Replace works as follows.
Code:
x = "hello replacement"
y = "chicken"
z = "hello"
k= x.replace(z,y)

Now when you print k, you will get "chicken replacement". So what has happened is this:
We've started with x("hello replacement"). and x.replace(z,y) will replace any occurrence of z("hello") with y("chicken"). So basically it replaces hello with chicken.

So I have

s.replace(x, subst[int(x)], 1)

To run if x is a digit.
I replace that digit with the letter in subst that has the corresponding index as x.
I set it to one so it does not replace every single occurrence but just the first.
 
Python strings are immutable objects.

So, you need to do s= s.replace(whatever)
 
I see now. You're code looks correct then, other than your lack of indentation. Make sure that it looks as follows along with jhae's suggestion
Code:
def substitude_digits(s, subst):

     for x in s:
          if x.isdigit() == True:
               s=s.replace(x, subst[int(x)], 1)

     return s

s = 'Hello 43212'
subst = 'xrehTlmnop'
print substitude_digits(s, subst)
 
dacruick said:
Code:
def substitute_digits(s, subst):

     for x in s:
          if x.isdigit() == True:
               s=s.replace(x, subst[int(x)], 1)

     return s

s = 'Hello 43212'
subst = 'xrehTlmnop'
print substitute_digits(s, subst)

I was able to run this successfully in Python 2.6.6.