Python Code Challenges 2, Strings, Count Letters

Hi everyone!

I’m currently doing the Computer Science career path and reached the second set of code challenges

In the first challenge, we are asked to write a code to count the letters in a word, without counting repeated letters and differentiating between upper and lower case letters.

This is my code:

letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

def unique_english_letters(word):
  count = 0

  for letter in letters:
    if letter in word:
      count += 1
  return count

This code is working, it is looping through the letters and adding to the “count” variable if the condition is satisfied. However, what I don’t understand is: how Python is differentiating the letters in the string “letters” if it is just a string, not a list? Is Python using a delimiter of some kind? In other words, how does Python knows when a letter “ends” and the next one “begins”?

Thank you in advance!

If it’s a list, the representation is broken up into single characters (still strings in python). So there’s no delimiting necessary.

When you have a regular string representation, python under the hood interprets each character as some sort of unicode. Unicode characters vary in size, so it picks the specific unicode subset which makes all the characters match in length. And then it’s just a PY_unicode *str (in cpython). You know the string is done because it has a \0 to delimit the end. For this to make sense you’d have to delve a bit into C, pointers, and strings in C.

1 Like

Thank you for your reply. I’ll research about C and pointers. But I think I could grasp a little bit how it works.

Thanks!