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

Replace characters in a string without loops

I tried using slicing but didn't succeed https://code.sololearn.com/cn3THw67hQap/?ref=app

25th Feb 2019, 6:46 PM
loleverytime okay
loleverytime okay - avatar
2 Answers
+ 1
Use regular expressions. Minimal use: if string in your code contains a date and you just want to replace "-" or "/" or anything else other than numbers import re num = re.sub(r'\D', "", string) print("Phone Num : ", num)
25th Feb 2019, 7:04 PM
Шащи Ранжан
Шащи Ранжан - avatar
+ 1
I think you want to do something like this: x='00001' x=x.replace('0','2') print(x) Output--> 22221
26th Feb 2019, 7:40 AM
Sahil Danayak
Sahil Danayak - avatar