🔍 Count Digits, Letters, and Spaces from a Sentence in Python | Using Regular Expressions
In this video, you'll learn how to count the number of digits, letters, and spaces in a given sentence using Python's re module (Regular Expressions). This is a common task in text processing and frequently asked in coding interviews.
📌 What you'll learn:
✅ How to filter and count digits from a string
✅ How to extract only alphabetic letters
✅ How to count spaces using regular expressions
✅ Real example with:
"python is 1 of the best programming language. 1 5 8"
🧠 Perfect for Python beginners, text analysis, and interview preparation.
🛠️ Code used in the video:
import re
str1 = "python is 1 of the best programming language. 1 5 8"
digit_count = re.sub(r'[^0-9]', '', str1)
letter_count = re.sub(r'[^a-zA-Z]', '', str1)
space_count = re.findall(r'[ \s]', str1)
print("Digit count:", len(digit_count))
print("Letter count:", len(letter_count))
print("Space count:", len(space_count))
💡 Tip: Use re.sub() to remove unwanted characters and re.findall() to collect matches.
👍 Like, 💬 Comment, 🔁 Share & 🔔 Subscribe for more Python text-processing tutorials and coding logic tips!
#Python #Regex #TextProcessing #CountDigits #CountLetters #CountSpaces #LearnPython #PythonForBeginners #PythonInterviewQuestions #PythonTips #PythonCoding