π Day 66/150 – Count Words in a String in Python
Counting words in a string is a common beginner-level Python problem and is very useful in text processing.
Example:
"Python is easy to learn" → 5 words
Let’s explore different methods to count words in Python π
πΉ Method 1 – Using split() and len()
text = "Python is easy to learn"
count = len(text.split())
print("Word Count:", count)
πΉ Method 2 – Taking User Input
text = input("Enter a string: ")
count = len(text.split())
print("Word Count:", count)
π Useful when taking dynamic input from users.
π Useful when taking dynamic input from users.
πΉ Method 3 – Using for Loop
✅ Output
Word Count: 5
π Counts spaces manually to estimate the number of words.
⚠️ This method works properly only when words are separated by a single space.
π Counts spaces manually to estimate the number of words.
⚠️ This method works properly only when words are separated by a single space.
πΉ Method 4 – Using Function
✅ Output

