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
Click For Summary

Discussion Overview

The discussion revolves around a Python programming task that involves creating a function to replace digits in a string with corresponding characters from another string. The focus is on the implementation details, debugging, and understanding the behavior of string methods in Python.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents an initial attempt at the function but struggles with the replace method and converting string digits to integers.
  • Another participant suggests using the int() function to convert a string to an integer.
  • A revised version of the function is shared, but the participant notes that the replace method still does not work as expected.
  • One participant explains how the replace method functions with an example, clarifying its usage.
  • Another participant emphasizes that strings in Python are immutable, suggesting that the result of the replace method needs to be reassigned to the string variable.
  • A later reply corrects the indentation in the code and confirms that the revised function works correctly in Python 2.6.6.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of the replace method and the immutability of strings, but there is no consensus on the initial implementation's effectiveness, as some participants continue to express confusion over its functionality.

Contextual Notes

Participants discuss the need for proper reassignment of the string after using the replace method, highlighting the importance of understanding Python's string handling. There are unresolved issues regarding the initial function's logic and effectiveness.

Who May Find This Useful

Individuals learning Python programming, particularly those interested in string manipulation and debugging techniques.

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

Replies
55
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K