DAY 09 — 60 DAY PYTHON CHALLENGE
💻 Word Counter
Python में Word Counter — शब्द, वाक्य, अक्षर गिनें
Advertisement
Complete Code
python — day09.py
def word_counter(): print("📝 Word Counter") print("Text लिखें (empty line = done):") print("-" * 35) lines = [] while True: line = input() if line == "": break lines.append(line) text = " ".join(lines) if not text.strip(): print("❌ कुछ तो लिखें!") return words = text.split() sentences = text.count(".") + text.count("!") + text.count("?") chars_total = len(text) chars_no_sp = len(text.replace(" ", "")) word_freq = {} for w in words: w = w.lower().strip(".,!?") word_freq[w] = word_freq.get(w, 0) + 1 top = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)[:3] print(f"\n📊 Results:") print(f" शब्द: {len(words)}") print(f" वाक्य: {sentences}") print(f" अक्षर (total): {chars_total}") print(f" अक्षर (बिना space): {chars_no_sp}") print(f" Top words: {[w for w,c in top]}") word_counter()
OUTPUT
📊 Results:
शब्द: 25
वाक्य: 3
अक्षर (total): 142
अक्षर (बिना space): 118
Top words: ['python', 'है', 'में']
यह Code कैसे काम करता है?
split() से words list बनती है। Dictionary से word frequency count होती है। sorted() with lambda से top 3 words निकलते हैं।
Advertisement