Chapter · String Algorithms Word Count 单词计数 ≈ 15 min · python-strings · Open in WolfCode One line. The lesson is which method to use — .split() vs .split(' '). Python · runnable Reset Run def word_count(text): return len(text.split()) # Default split handles all the edge cases gracefully: print(word_count("hello world")) # 2 print(word_count(" hello world ")) # 2 (extra spaces collapsed) print(word_count("a\tb\nc")) # 3 (tabs / newlines work too) print(word_count("")) # 0 print(word_count(" ")) # 0 Loading Python runtime (~5 MB, one-time)… Common wrong answers Python · runnable Reset Run text = "hello world" print(len(text)) # 11 — that's the CHARACTER count, not words print(text.count(" ")) # 1 — that's the SPACE count, also wrong Loading Python runtime (~5 MB, one-time)… ← Previous Walkthrough: Count Vowels Next → Palindrome Check