Python program to calculate the number of words and characters in the string Last Updated : 16 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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. In this case, it will be total words will be 6 and total characters will be 45. To find these we can use methods like split() and regex and many more.Using split()split function is quite useful and usually quite a generic method to get words out of the list but this approach fails once we introduce special characters in the list. Python s = "Geeksforgeeks is best Computer Science Portal" # using split() # to count words in string res = len(s.split()) # printing result print("The number of words in string are : " + str(res)) print("The number of words in string are : ", len(s)) OutputThe number of words in string are : 6 The number of words in string are : 45 Explanation:Split() method divides the string into words using whitespace as the delimiter, and len() counts the number of elements in the resulting list. This accurately gives the word count.len(s) counts the total number of characters in the string, not the words, leading to an incorrect result.Using regex modulefindall() function is used to count the number of words in the sentence available in a regex module. Python import re s = "GeeksForGeeks is a learning platform" # using regex (findall()) function res = len(re.findall(r'\w+', s)) # total no of words print("The number of words in string are : " + str(res)) print("The number of Characters in string are : ", len(s)) OutputThe number of words in string are : 5 The number of Characters in string are : 36 Explanation:re.findall(r'\w+', s) function uses a regex pattern to match all sequences of alphanumeric characters in the string. The length of the resulting list gives the total number of words.len(s) counts all characters in the string, including spaces, resulting in the total character count.Using sum()+ strip()+ split() functionHere we first check all the words in the given sentence and add them using the sum() function. Python import string s = "GeeksForGeeks is a learning platform" # using sum() + strip() + split() function res = sum([i.strip(string.punctuation).isalpha() for i in s.split()]) # no of words print("The number of words in string are : " + str(res)) print("The number of characters in string are : ", len(s)) OutputThe number of words in string are : 5 The number of characters in string are : 36 Explanation:Code splits the string into words, strips punctuation using string.punctuation, and checks if each stripped word contains only alphabetic characters with isalpha(). The sum() adds up True values (counted as 1) for all valid words.len(s) calculates the total number of characters in the string, including letters, spaces, and punctuation. Comment More infoAdvertise with us Next Article Python program to calculate the number of words and characters in the string biswasarkadip Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python program to calculate the number of digits and letters in a string 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.Pythons = "Hello123!" # Initialize cou 3 min read Python Program to Count the Number of Vowels in a String In this article, we will be focusing on how to print each word of a sentence along with the number of vowels in each word using Python. Vowels in the English language are: 'a', 'e', 'i', 'o', 'u'. So our task is to calculate how many vowels are present in each word of a sentence. So let us first des 10 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 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 Find the Number of Unique Words in Text File Given a text file, write a python program to find the number of unique words in the given text file in Python.Examples:Input: gfg.txtOutput: 18Contents of gfg.txt: GeeksforGeeks was created with a goal in mind to provide well written well thought and wellexplained solutions for selected questionsExp 2 min read Like