+ 1

replace a character python

sentence = ' mam a a mam ' sentence = re.sub(' a ', ' ', sentence) print(sentence) output: mam a mam expected output: mam mam is there a short and simple solution to this problem??

3rd Sep 2021, 3:43 PM
Kairav Bhatia
Kairav Bhatia - avatar
4 ответов
+ 1
Calvin [Inactive] thanks but is there a way to use metacharactors to do this??
3rd Sep 2021, 4:37 PM
Kairav Bhatia
Kairav Bhatia - avatar
0
re.sub() matches all occurences of the pattern if no count parameter has been set. So pay attention to the blanks in your pattern and replacement strings. They should be " a" and an empty string, just two quotes without anything in between : ''. I deleted some blanks, tried your code and got the expected output.
3rd Sep 2021, 4:04 PM
Delorme
0
Delorme can u please share the program??
3rd Sep 2021, 4:38 PM
Kairav Bhatia
Kairav Bhatia - avatar
0
Kairav Bhatia Here you are ; I just changed ' a ' to ' a' and ' ' to '' : https://code.sololearn.com/c6S1L4Wm1b91/?ref=app
4th Sep 2021, 6:14 PM
Delorme