Python program to calculate the number of digits and letters in a string Last Updated : 09 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will check various methods to calculate the number of digits and letters in a string. Using a for loop to remove empty strings from a list involves iterating through the list, checking if each string is not empty, and adding it to a new list. Python s = "Hello123!" # Initialize counters for letters and digits l_c = 0 d_c = 0 # Iterate through each character in the string for char in s: # Check if the character is a letter if char.isalpha(): l_c += 1 # Check if the character is a digit elif char.isdigit(): d_c += 1 print(f"Letters: {l_c}") print(f"Digits: {d_c}") OutputLetters: 5 Digits: 3 Explanationfor loop iterates through each character in the string s, using char.isalpha() to check if a character is a letter and char.isdigit() to check if it is a digit.counters l_c and d_c are incremented based on the character type, and the final counts of letters and digits are printed.Using filter()Using filter() to remove empty strings from a list allows you to apply a filtering condition, such as checking whether each string is non-empty, and return a list of only the strings that meet the condition. Python s = "Hello123!" # Count letters using filter and len l_c = len(list(filter(str.isalpha, s))) # Count digits using filter and len d_c = len(list(filter(str.isdigit, s))) print(f"Letters: {l_c}") print(f"Digits: {d_c}") OutputLetters: 5 Digits: 3 Explanationfilter(str.isalpha, s) filters out only the alphabetic characters from the string s, and len(list(...)) counts the number of alphabetic characters.filter(str.isdigit, s) filters out the digits, and len(list(...)) counts the number of digits in the string, with both counts being printed.Using collections.CounterUsing collections.Counter to count the number of letters and digits in a string allows to check the occurrences of each character. The Counter class creates a dictionary-like object where characters are keys, and their counts are values. Python from collections import Counter # Input string s = "Hello123!" # Count frequencies of each character f = Counter(s) # Count letters and digits l_c = sum(1 for char in f if char.isalpha()) d_c = sum(1 for char in f if char.isdigit()) # Print the counts print(f"Letters: {l_c}") print(f"Digits: {d_c}") OutputLetters: 4 Digits: 3 ExplanationThe Counter(s) counts the frequency of each character in the string s, storing the results in a Counter object f.The sum(1 for char in f if char.isalpha()) counts the number of alphabetic characters, and sum(1 for char in f if char.isdigit()) counts the number of digits in the string, with the counts being printed. Comment More infoAdvertise with us Next Article Python program to calculate the number of digits and letters in a string abhigoya Follow Improve Article Tags : Technical Scripter Python Python Programs Technical Scripter 2020 Python string-programs +1 More Practice Tags : python Similar Reads Python program to calculate the number of words and characters in the string We are given a string we need to find the total number of words and total number of character in the given string.For Example we are given a string s = "Geeksforgeeks is best Computer Science Portal" we need to count the total words in the given string and the total characters in the given string. I 3 min read Python program to check if a string has at least one letter and one number The task is to verify whether a given string contains both at least one letter (either uppercase or lowercase) and at least one number. For example, if the input string is "Hello123", the program should return True since it contains both letters and numbers. On the other hand, a string like "Hello" 3 min read Python program to count the number of spaces in string In Python, there are various ways to Count the number of spaces in a String.Using count() Methodcount() method in Python is used to return the number of occurrences of a specified element in a list or stringPythons = "Count the spaces in this string." # Count spaces using the count() method space_co 3 min read Python Program to test if the String only Numbers and Alphabets Given a String, our task is to write a Python program to check if string contains both numbers and alphabets, not either nor punctuations. Examples: Input : test_str = 'Geeks4Geeks' Output : True Explanation : Contains both number and alphabets. Input : test_str = 'GeeksforGeeks' Output : False Expl 4 min read Python Program to move numbers to the end of the string Given a string, the task is to write a Python program to move all the numbers in it to its end. Examples: Input : test_str = 'geek2eeks4g1eek5sbest6forall9' Output : geekeeksgeeksbestforall241569 Explanation : All numbers are moved to end. Input : test_str = 'geekeeksg1eek5sbest6forall9' Output : ge 6 min read Count the number of characters in a String - Python The goal here is to count the number of characters in a string, which involves determining the total length of the string. For example, given a string like "GeeksForGeeks", we want to calculate how many characters it contains. Letâs explore different approaches to accomplish this.Using len()len() is 2 min read Get number of characters, words, spaces and lines in a file - Python Given a text file fname, the task is to count the total number of characters, words, spaces, and lines in the file. As we know, Python provides multiple in-built features and modules for handling files. Let's discuss different ways to calculate the total number of characters, words, spaces, and line 5 min read Python Program to Find Sum of First and Last Digit Given a positive integer N(at least contain two digits). The task is to write a Python program to add the first and last digits of the given number N. Examples: Input: N = 1247 Output: 8 Explanation: First digit is 1 and Last digit is 7. So, addition of these two (1 + 7) is equal to 8.Input: N = 73 5 min read Python Program for Check if all digits of a number divide it Given a number n, find whether all digits of n divide it or not. Examples: Input : 128Output : Yes128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Input : 130Output : No We want to test whether each digit is non-zero and divides the number. For example, with 128, we want to test d != 0 && 128 % 3 min read Python program to Increment Suffix Number in String Given a String, the task is to write a Python program to increment the number which is at end of the string. Input : test_str = 'geeks006' Output : geeks7 Explanation : Suffix 006 incremented to 7. Input : test_str = 'geeks007' Output : geeks8 Explanation : Suffix 007 incremented to 8. Method #1 : U 5 min read Like