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

  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    Beginner Python
AI Thread Summary
The discussion revolves around creating a Python function, `substitute_digits`, which replaces digits in a string with corresponding characters from a substitution string. The function should replace '0' with the first character of the substitution string, '1' with the second character, and so forth, up to '9' with the last character. Initial attempts to implement the function faced issues with the `replace` method not modifying the string as expected. Participants clarified that strings in Python are immutable, meaning that any modifications must be assigned back to the string variable. The correct approach involves iterating through each character in the input string, checking if it's a digit, and then using `s.replace()` to replace it with the appropriate character from the substitution string. The final code provided correctly implements these principles, ensuring that the modified string is returned after all replacements. The function was successfully tested in Python 2.6.6, confirming its functionality.
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.
 
Technology 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.
 

Similar threads

Back
Top