-3

I was working with the .count method for Strings. The count method finds the total number of occurrences of the argument inside the calling string.

But I don't understand how .count("") works. Can someone explain that?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
rmutalik
  • 1,925
  • 3
  • 16
  • 20
  • 1
    Try `"".count("")`. Now try `"a".count("")`. Now try `"aa".count("")`. Now try `"aaa".count("")`. Now try `"aaaa".count("")`. – Mateen Ulhaq Jun 03 '18 at 06:08

2 Answers2

1

There are three offsets – distances from the start of the string – where you can find an empty string: 0, 1, and 2.

| a | A |
^0  ^   ^
    |1  |
        |2

You can find an empty string at any offset for any string, of course, so s.count("") is always len(s) + 1.

Ry-
  • 218,210
  • 55
  • 464
  • 476
1

The empty string occurs three times in "aA": once before the "a", once between the "a" and the "A" and once after the "A".

NickD
  • 5,937
  • 1
  • 21
  • 38