Question

Write a function called double_str that takes a string argument and returns a new string that...

Write a function called double_str that takes a string argument and returns a new string that has all of the same characters as the argument, except each one is repeated twice. For example:

double_str('dog') --> 'ddoogg'

looking for python coding below is what I have so far

double_str = 'robert'

double_str 'robert'

for i in range(len(double_str)):

print(double_str[i])

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Assuming the language is Python. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required. Also this method uses iterative approach. If you are looking for a recursive method, just let me know, I’ll update the code quickly.

#code

#required method
def double_str(text):
    #creating an empty string to store the result
    updated = ''
    #looping through each character in text
    for c in text:
        #appending c to updated, twice
        updated += c + c
    #returning the updated string
    return updated


#testing
print(double_str('dog'))  # ddoogg
print(double_str('hello'))  # hheelllloo
Add a comment
Know the answer?
Add Answer to:
Write a function called double_str that takes a string argument and returns a new string that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT