why I can't understand str.replace() function ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

why I can't understand str.replace() function ?

print("abc".replace("","|")) print("".replace("","abc",3)) #no output why? run the code I posed below and explain it to me I am really unable to understand it... so confusing... https://code.sololearn.com/cf5Trk3ULj8P/?ref=app

13th Oct 2020, 1:35 PM
Ratnapal Shende
Ratnapal Shende - avatar
10 Answers
+ 9
replace() function does not modify the string that should be changed in place, but returns a new string. txt = "hello" new = txt.replace("l","x") # all characters "l" will be replaced by character "x" print(new) # result is: "hexxo" # with python 3.8 you can do it a bit more simpler with the walrus operator print(new := txt.replace("l","x") The string / substring to replace and the replacement itself are not limited to single characters.
13th Oct 2020, 2:24 PM
Lothar
Lothar - avatar
+ 4
mohammad hosein norouzi Why do u post the same answer that Jayakrishna posted 1 day earlier..?
15th Oct 2020, 5:37 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 3
Jayakrishna🇮🇳 the thing is that : - "" in "" is True - "".count("") == 1 - "".replace("", "abc") == "abc" BUT - "".replace("", "abc", n) == "" His question is : why when you specify the last argument of replace (by default -1), it suddenly behaves differently?
13th Oct 2020, 3:15 PM
Théophile
Théophile - avatar
+ 2
Replace function takes first argument as regex. So replace function consider a string abc as [ '', 'a', '', 'b', '','c', '' ] , here ''(single quotes) empty charecter. This is strange outcome by regex.. So every empty charecter is replaced by 'l'. In 2nd statement the 3 is about maximum number of replacements to be taken place.. So range replacements are 0 to 3(inclusive).
13th Oct 2020, 5:00 PM
Jayakrishna 🇮🇳
+ 1
I agree, this is a weird behaviour... And "".count("") returns 1. It's very confusing, indeed.
13th Oct 2020, 2:14 PM
Théophile
Théophile - avatar
+ 1
Alphin K Sajan Lothar i know str.replace() function will you explain the lines that I posted above?
13th Oct 2020, 2:22 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 1
Ratnapal Shende apparently, it is a normal behaviour of replace. This 'bug' will be resolved in Python3.9, so that : - "".replace("", "abc") And - "".replace("", "abc", n) Will give the same output. So we have to wait for Python3.9 😉
13th Oct 2020, 3:31 PM
Théophile
Théophile - avatar
0
2nd string is empty string. It don't have any empty charecters even so doesn't output anything.. Doesn't replace anything also....
13th Oct 2020, 2:50 PM
Jayakrishna 🇮🇳
0
Théophile OK bro Thank you so much! please explain the first line of my code also
13th Oct 2020, 3:57 PM
Ratnapal Shende
Ratnapal Shende - avatar
0
Replace function takes first argument as regex. So replace function consider a string abc as [ '', 'a', '', 'b', '','c', '' ] , here ''(single quotes) empty charecter. This is strange outcome by regex.. So every empty charecter is replaced by 'l'. In 2nd statement the 3 is about maximum number of replacements to be taken place.. So range replacements are 0 to 3(inclusive). Good luck.
15th Oct 2020, 5:31 AM
mohammad hosein norouzi
mohammad hosein norouzi - avatar