Count Total Words in Array of Strings

Count total words across all strings.

Logic BuildingIntermediate
Logic Building
# Take array of strings
n = int(input("Enter number of strings: "))
arr = []
for i in range(n):
    arr.append(input(f"String {i+1}: "))

# Count words
total_words = 0
for s in arr:
    words = s.split()
    total_words += len(words)

print(f"Total words: {total_words}")

Output

Enter number of strings: 3
String 1: Hello World
String 2: Programming
String 3: Python Java
Total words: 5

Split each string and count words.

Key Concepts:

  • Split each string
  • Count words in each
  • Sum all counts