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

In summary, the function substitutes each digit in a string with the second character of a subst string.
  • #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.
 
Technology news on Phys.org
  • #2
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)
 
  • #3
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.
 
  • #4
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:
  • #5
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.
 
  • #6
Python strings are immutable objects.

So, you need to do s= s.replace(whatever)
 
  • #7
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)
 
  • #8
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.
 

1. What is Python and what is it used for?

Python is a high-level, interpreted programming language that is widely used in various fields such as web development, data science, artificial intelligence, and more. It is known for its simple and easy-to-learn syntax, making it a popular choice for beginners and experienced programmers alike.

2. How do I install Python on my computer?

The official Python website provides installation instructions for various operating systems. Simply go to https://www.python.org/downloads/ and select the appropriate installer for your system. You can also use package managers like pip or Anaconda to install Python and its libraries.

3. What is the difference between Python 2 and Python 3?

Python 3 is the newer version of Python and has many improvements and new features compared to Python 2. Some of the key differences include improved Unicode support, better handling of exceptions, and the introduction of new data types such as the "bytes" type. It is recommended to use Python 3 for new projects, as Python 2 will no longer be supported after 2020.

4. How do I write a simple "Hello World" program in Python?

To print "Hello World" on the screen, you can use the built-in function "print()" in Python 3, like this:

print("Hello World")

If you are using Python 2, you will need to use the "print" statement instead:

print "Hello World"

5. Where can I find resources to learn Python?

There are many online resources available to learn Python, such as tutorials, documentation, and video courses. Some popular websites include Codecademy, Coursera, and Real Python. You can also join online communities or attend local meetups to connect with other Python learners and programmers.

Similar threads

  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top