-2

I am a beginner in python ,I came across a problem in understanding count fun in python.

txt = "hi"
x = txt.count("")
y = len(txt)
print("output for fun count",x)
print("output for fun len" , y)

The output for count is 3,Friends let know why I got the answer 3,how does it ended up with that value.

  • 1
    This question was already asked. https://stackoverflow.com/questions/40192449/why-are-str-count-and-lenstr-giving-different-output – Yuri Ginsburg Jan 02 '22 at 07:18
  • What answer do you think you should get instead? Why? Why does it make sense to you, to use `.count` in this way? – Karl Knechtel Jan 02 '22 at 07:42

2 Answers2

0

Count function counts the appearance of value in the string As much as I know. You are counting apperance of empty string. "" Appears 3 times, 1st time before "h", 2nd time between "h" and "i", 3rd time after "i". As much as I know

NNM
  • 358
  • 1
  • 10
0

In your case, it counts the “gaps” in the string. That is the number of characters + 1

A. Guy
  • 500
  • 1
  • 3
  • 10