[Solved] How do I count the number of characters in a code using accumulation pattern without using len()?


If you cannot use the len() function you could write a function like num_characters below that uses a for loop to iterate over the characters in the passed in string and increments and subsequently returns a variable total based on the total amount of characters. I think that is what you mean by an accumulator right?

def num_characters(string):
  total = 0
  for character in string:
    total += 1
  return total

original_string = "The quick brown rhino jumped over the extremely lazy fox."
print(f"The numbers of characters in the original string using `len` is {len(original_string)}.")
print(f"The numbers of characters in the original string using `num_characters` is {num_characters(original_string)}.")

Output:

The numbers of characters in the original string using `len` is 57.
The numbers of characters in the original string using `num_characters` is 57.

solved How do I count the number of characters in a code using accumulation pattern without using len()?