String replace method...though it was easy | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

String replace method...though it was easy

Took this one from a challenge: >>> a = "abcefd" >>> print(a.replace('cd','12')) Output: 'abcefd' # why is that??????? I was expecting --> 'ab1ef2' . Please explain a.replace('cd','12') is 'cd' given as an interval from c to d included, so 'cefd' , since it is not recognized as a substring??? Or shall I consider 'cd' as separate 'c' and 'd' ? Usually if you look for tutorials about replace() method on strings, the typical example are always given with substrings of the original string, never seen something like that above.

16th Oct 2018, 8:39 AM
Roberto
1 Answer
+ 2
It replace a substring 'cd' with string '12' but because in your source string ('abcdefd') its not present the substring 'cd', no replacement happen
16th Oct 2018, 9:01 AM
KrOW
KrOW - avatar