0

Can someone explain me the following :-

Suppose i have this String variable

pcalpha = 'abcdefghijklmnopqrstuvwxyz'

Writing this piece of code print(len(pcalpha)) will result in an output of an int which is equal to 26 which from my understanding means that the string has 26 characters in it.

Writing this other piece of code print(pcalpha.count("")) shall also give the same result according to the logic am following, but it is not. Can someone tell me why is it showing 27?

1

1 Answer 1

0

The intuition is that there is a "" after each letter and one before the first letter. So if you have 26 letters, that means you have 26 "" plus the one at the beginning 26 + 1 = 27. For an example, let's replace "" by "-":

pcalpha = 'abcdefghijklmnopqrstuvwxyz'
print(pcalpha.replace("", "-"))

Output

-a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z-

Further

  1. For the implementation details, as mentioned by @RandomDavis, see this.
  2. Replacing the empty strings in a string
Sign up to request clarification or add additional context in comments.

5 Comments

To see why it returns 27 instead of 26, you can see the implementation here: github.com/python/cpython/blob/…
Yeah sure, i will refer to that as soon as i advance in the subject, currently i can barely understand anything.
If i may ask, what does the "" actually mean in python?
@SaifYsn It means the empty string
There is an argument that there are an infinite number of instances of the empty string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.