{"id":10217,"date":"2021-09-30T12:00:56","date_gmt":"2021-09-30T06:30:56","guid":{"rendered":"https:\/\/python-programs.com\/?p=10217"},"modified":"2021-11-22T18:34:39","modified_gmt":"2021-11-22T13:04:39","slug":"python-program-to-calculate-the-number-of-words-and-the-number-of-characters-present-in-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-calculate-the-number-of-words-and-the-number-of-characters-present-in-a-string\/","title":{"rendered":"Python Program to Calculate the Number of Words and the Number of Characters Present in a String"},"content":{"rendered":"
Strings in Python:<\/strong><\/p>\n In Python, a string\u00a0may be a\u00a0<\/span>sequence of characters. It is\u00a0an information<\/span>\u00a0type that has been derived. Strings are unchangeable. This means\u00a0that when\u00a0<\/span>they have been\u00a0<\/span>defined,\u00a0they can not\u00a0<\/span>be modified. Many Python functions change strings,\u00a0like\u00a0<\/span>replace(), join(), and split(). They do not, however, alter the original string. They make\u00a0a duplicate<\/span>\u00a0of a string, alter it, then return it to the caller.<\/p>\n Given a string, the task is to count the words and the total number of characters in the given string in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to count the total number of words and characters in the given string in python some of them are:<\/p>\n Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples<\/a> and try to code all of them on your own then check with the exact code provided by expert programmers.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The output is the total number of characters and words present in the given string.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Strings in Python: In Python, a string\u00a0may be a\u00a0sequence of characters. It is\u00a0an information\u00a0type that has been derived. Strings are unchangeable. This means\u00a0that when\u00a0they have been\u00a0defined,\u00a0they can not\u00a0be modified. Many Python functions change strings,\u00a0like\u00a0replace(), join(), and split(). They do not, however, alter the original string. They make\u00a0a duplicate\u00a0of a string, alter it, then return it …<\/p>\ngiven string ='Hello this is btechgeeks'<\/pre>\n
Total characters present in given string { Hello this is btechgeeks } = 24\r\nTotal words present in given string { Hello this is btechgeeks } = 4<\/pre>\n
given string ='hello this is btechgeeks online platform for coding students'<\/pre>\n
Total characters present in given string { hello this is btechgeeks online platform for coding students } = 61\r\nTotal words present in given string { hello this is btechgeeks online platform for coding students } = 9<\/pre>\n
Program to Calculate the Number of Words and the Number of Characters Present in a String<\/h2>\n
\n
Method #1:Using Count Variable(Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngiven_string = 'Hello this is btechgeeks'\r\n# Take a variable to say stringchars that store the total characters in the given string.\r\n# Initialize the stringchars to 0.\r\nstringchars = 0\r\n# Take a variable to say stringwords that stores the total words in the given string.\r\n# Initialize the stringwords to 1.\r\nstringwords = 1\r\n# To traverse the characters in the string, use a For loop.\r\nfor charact in given_string:\r\n # If a space character is encountered then increment the stringwords by 1.\r\n if(charact == ' '):\r\n stringwords = stringwords+1\r\n # increment the stringchars variable each time by 1.\r\n stringchars = stringchars+1\r\n# Print the total words and character present in the given string\r\nprint('Total characters present in given string {', given_string, '} =', stringchars)\r\nprint('Total words present in given string {', given_string, '} =', stringwords)\r\n<\/pre>\n
Total characters present in given string { Hello this is btechgeeks } = 24\r\nTotal words present in given string { Hello this is btechgeeks } = 4<\/pre>\n
\n
Method #2:Using Count Variable(User Input)<\/h3>\n
\n
# Give the string as user input using input() function and store it in a variable.\r\ngiven_string = input('Enter some random string =')\r\n# Take a variable to say stringchars that store the total characters in the given string.\r\n# Initialize the stringchars to 0.\r\nstringchars = 0\r\n# Take a variable to say stringwords that stores the total words in the given string.\r\n# Initialize the stringwords to 1.\r\nstringwords = 1\r\n# To traverse the characters in the string, use a For loop.\r\nfor charact in given_string:\r\n # If a space character is encountered then increment the stringwords by 1.\r\n if(charact == ' '):\r\n stringwords = stringwords+1\r\n # increment the stringchars variable each time by 1.\r\n stringchars = stringchars+1\r\n# Print the stringLength.\r\nprint(\r\n 'Total characters present in given string {', given_string, '} =', stringchars)\r\nprint(\r\n 'Total words present in given string {', given_string, '} =', stringwords)\r\n<\/pre>\n
Enter some random string =hello this is btechgeeks online platform for coding students \r\nTotal characters present in given string { hello this is btechgeeks online platform for coding students } = 61\r\nTotal words present in given string { hello this is btechgeeks online platform for coding students } = 9\r\n\r\n<\/pre>\n
Method #3:Using split() and len() functions(Static Input)<\/h3>\n
\n
# Give\u00a0the\u00a0string\u00a0as\u00a0static\u00a0input\u00a0and\u00a0store\u00a0it\u00a0in\u00a0a\u00a0variable.\r\ngiven_string = 'hello this is btechgeeks'\r\n# Split the given string into a list of words using list() and split() functions.\r\nlistwords = given_string.split()\r\n# The length of the list gives the total number of words in the given string and\r\n# store it in a variable say stringwords.\r\nstringwords = len(listwords)\r\n# The length of the given string is calculated using the len() function which\r\n# gives the total number of characters present in the given string.\r\nstringLeng = len(given_string)\r\n# Store this characters count in a variable say stringchars.\r\nstringchars = stringLeng\r\n#Print the total words and character present in the given string\r\nprint('Total characters present in given string {', given_string, '} =', stringchars)\r\nprint('Total words present in given string {', given_string, '} =', stringwords)\r\n<\/pre>\n
Total characters present in given string { hello this is btechgeeks } = 24\r\nTotal words present in given string { hello this is btechgeeks } = 4<\/pre>\n
Method #4:Using split() and len() functions(User Input)<\/h3>\n
\n
# Give the string as user input using input() function and store it in a variable.\r\ngiven_string = input('Enter some random string = ')\r\n# Split the given string into a list of words using list() and split() functions.\r\nlistwords = given_string.split()\r\n# The length of the list gives the total number of words in the given string and\r\n# store it in a variable say stringwords.\r\nstringwords = len(listwords)\r\n# The length of the given string is calculated using the len() function which\r\n# gives the total number of characters present in the given string.\r\nstringLeng = len(given_string)\r\n# Store this characters count in a variable say stringchars.\r\nstringchars = stringLeng\r\n# Print the total words and character present in the given string\r\nprint('Total characters present in given string {', given_string, '} =', stringchars)\r\nprint('Total words present in given string {', given_string, '} =', stringwords)\r\n<\/pre>\n
Enter some random string = hello this is btechgeeks online platform for coding students\r\nTotal characters present in given string { hello this is btechgeeks online platform for coding students } = 61\r\nTotal words present in given string { hello this is btechgeeks online platform for coding students } = 9<\/pre>\n
\n