-1

Need to know the reason for the below observations in python 3.7

1>

"abc".count('') 

returns 4 2>

"" in "abc" 

returns True 3>

"abc".find("") 

returns 0

I tried this and got the below results

enter image description here

addy
  • 1
  • 4
  • It seem that `'abc'` is regarded as `'' + 'a' + '' + 'b' + '' + 'c' + ''` – Mechanic Pig Feb 25 '23 at 08:39
  • The empty string is a substring of every string, including the empty string. If you're familiar with set theory, this is natural. If you're not, it might not be. – molbdnilo Feb 25 '23 at 08:52
  • Some parts of Python strings are a bit weird. One thing I've learned is to always check the documentation. – molbdnilo Feb 25 '23 at 09:07

2 Answers2

0

Each character in the string "abc" is separated by an empty string. Therefore, the empty string appears between each character and at the beginning and end of the string. So, there are four empty strings in the string "abc":

"" + "a" + "" + "b" + "" + "c" + ""
CodeX
  • 55
  • 6
0

You are looking for an empty string "".

count() counts all possible possitions. Which are "|a|b|c|".

The line "" in "abc" is looking for at least one possition.

And find() returns the possition for the first match.

mosc9575
  • 5,618
  • 2
  • 9
  • 32