In this article, we will discuss the concept of the Python program to Count words, character and Space in a string
In this post, we are going to learn how to count words, character and space of the given String in Python programming language
The program allows the user to enter a String and then it counts and display the total number of words, character and Space of the given string using for loop in Python programing language
Program 1
str=raw_input("Enter String") #Python 2,7 char=0 word=1 for i in str: char=char+1 if(i==' '): word=word+1 print("Number of words in the given string ",word) print("Number of characters in the given string ",char) print("Number of space in the given string ",(word-1))
When the above code is executed, it produces the following result
Enter String: Python programming language Number of words in the given string 3 Number of characters in the given string 27 Number of space in the given string 2
Approach
Program 2
The program allows the user to enter a String and then it counts and display the total number of words, character and Space of the given string using the built-in function in Python programing language
wordCount=0#Python 3.0 charCount=0 str=input("Enter the string\n") split_str=str.split() wordCount=len(split_str) for word in split_str: charCount+=len(word) print("Total words in the given string ",wordCount) print("Total characters in the given string ",charCount) print("Number of space in the given string ",(wordCount-1))
When the above code is executed, it produces the following result
Enter the string Basic Python language Total words in the given string 3 Total characters in the given string 19 Number of space in the given
Suggested for you
Similar post
C++ code to count the total number of characters in the given string
C code to count the total number of characters in the given string
Python code to count the total number of characters in the given string
Java program to count the total number of characters in the given string including space
C program to count the total number of characters in the given string including space
C++ program to count the total number of characters in the given string including space
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…