Python program to calculate the number of words and characters in the string
Given a String. The task is to find out the Number Of Words And Characters Present In The String.
Examples:
Input: Geeksforgeeks is best Computer Science Portal Output: The number Of Words are : 6 The Number Of Characters are : 45 Input: Hello World!!! Output: The original string is : Hello World!!! The number of words in string are : 2 The number of words in string are : 14
Count The Number Of Characters present in a string using len() function. You can also use a for loop for counting characters
char=0
for i in string:
char=char+1For Counting
Method 1: Using split()
The 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.
Python3
# Python3 code to demonstrate# to count words in string# using split()# initializing stringtest_string = "Geeksforgeeks is best Computer Science Portal"# printing original stringprint("The original string is : " + test_string)# using split()# to count words in stringres = len(test_string.split())# printing resultprint("The number of words in string are : " + str(res))print("The number of words in string are : ", len(test_string)) |
Output:
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
The number of words in string are : 45
Method 2: Using regex module
Here findall() function is used to count the number of words in the sentence available in a regex module.
Python3
import retest_string = "GeeksForGeeks is a learning platform"# original stringprint("The original string is : " + test_string)# using regex (findall()) functionres = len(re.findall(r'\w+', test_string))# total no of wordsprint("The number of words in string are : " + str(res))print("The number of Characters in string are : ", len(test_string)) |
Output:
The original string is : GeeksForGeeks is a learning platform
The number of words in string are : 5
The number of Characters in string are : 36
Method 3: Using sum()+ strip()+ split() function
Here we first check all the words in the given sentence and add them using the sum() function.
Python3
import stringtest_string = "GeeksForGeeks is a learning platform"# printing original stringprint("The original string is: " + test_string)# using sum() + strip() + split() functionres = sum([i.strip(string.punctuation).isalpha() for i in test_string.split()])# no of wordsprint("The number of words in string are : " + str(res))print("The number of characters in string are : ", len(test_string)) |
Output:
The original string is: GeeksForGeeks is a learning platform
The number of words in string are : 5
The number of characters in string are : 36



