Replace Characters in a String | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Replace Characters in a String

I have a string, "hello" but let's say the string is unknown. I want to replace every character in that string x amount of times. Can anyone help me to do that please, thank you.

25th Jun 2020, 10:38 AM
Kirill
Kirill - avatar
10 Answers
+ 3
Kirill Vidov, can you please complete the mentioned sample by showing us the desired output? Thanks!
25th Jun 2020, 12:56 PM
Lothar
Lothar - avatar
+ 2
Yeah, you can take a look at my code.. https://code.sololearn.com/cyQB56iRtg01/?ref=app
25th Jun 2020, 10:41 AM
Blue!!
Blue!! - avatar
+ 1
Blue!! Iam writing the code in python
25th Jun 2020, 10:42 AM
Kirill
Kirill - avatar
+ 1
As strings are immutable,we can not modify the existing string. But we can achieve it by using loops ,another strings
25th Jun 2020, 10:43 AM
uday kiran
uday kiran - avatar
+ 1
Blue!! I tried doing it with a for loop but if I have a string with duplicates it's only going to replace the duplicates and leave the rest ones
25th Jun 2020, 10:49 AM
Kirill
Kirill - avatar
0
Oops, sorry but they are pretty similar
25th Jun 2020, 10:43 AM
Blue!!
Blue!! - avatar
0
You can use python built in replace() function
25th Jun 2020, 10:47 AM
Blue!!
Blue!! - avatar
0
You can try this: text = 'hello oorld' replace = list(text) replace[6] = 'W' ''.join(replace)
25th Jun 2020, 10:59 AM
Blue!!
Blue!! - avatar
0
can u give sample Input and desired output as example
25th Jun 2020, 8:35 PM
Suhail KM
0
str="hello" print(str.replace("h","k")) for n times , you may run a loop
26th Jun 2020, 7:56 AM
Hima
Hima - avatar