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
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…