Program to Calculate the Number of Words and the Number of Characters Present in a String

Python Program to Calculate the Number of Words and the Number of Characters Present in a String

Strings in Python:

In Python, a string may be a sequence of characters. It is an information type that has been derived. Strings are unchangeable. This means that when they have been defined, they can not be modified. Many Python functions change strings, like replace(), join(), and split(). They do not, however, alter the original string. They make a duplicate of a string, alter it, then return it to the caller.

Given a string, the task is to count the words and the total number of characters in the given string in Python.

Examples:

Example1:

Input:

given string ='Hello this is btechgeeks'

Output:

Total characters present in given string { Hello this is btechgeeks } = 24
Total words present in given string { Hello this is btechgeeks } = 4

Example2:

Input:

given string ='hello this is btechgeeks online platform for coding students'

Output:

Total characters present in given string { hello this is btechgeeks online platform for coding students } = 61
Total words present in given string { hello this is btechgeeks online platform for coding students } = 9

Program to Calculate the Number of Words and the Number of Characters Present in a String

There are several ways to count the total number of words and characters in the given string in python some of them are:

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Method #1:Using Count Variable(Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Take a variable to say stringwords that stores the total words in the given string.
  • Initialize the stringwords to 1.
  • Take a variable to say stringchars that store the total characters in the given string.
  • Initialize the stringchars to 0.
  • To Traverse, the characters in the string, use a for loop and increment the stringchars variable each time by 1.
  • If a space character is encountered then increment the stringwords by 1.
  • Print the total number of characters and words in the string i.e stringchars and stringwords.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
given_string = 'Hello this is btechgeeks'
# Take a variable to say stringchars that store the total characters in the given string.
# Initialize the stringchars to 0.
stringchars = 0
# Take a variable to say stringwords that stores the total words in the given string.
# Initialize the stringwords to 1.
stringwords = 1
# To traverse the characters in the string, use a For loop.
for charact in given_string:
   # If a space character is encountered then increment the stringwords by 1.
    if(charact == ' '):
        stringwords = stringwords+1
     # increment the stringchars variable each time by 1.
    stringchars = stringchars+1
# Print the total words and character present in the given string
print('Total characters present in given string {', given_string, '} =', stringchars)
print('Total words present in given string {', given_string, '} =', stringwords)

Output:

Total characters present in given string { Hello this is btechgeeks } = 24
Total words present in given string { Hello this is btechgeeks } = 4

Explanation:

  • A string must be entered by the user as static input and saved in a variable.
  • 2. The character count variable is set to zero, and the word count variable is set to one (to account for the first word).
  •  The for loop is used to go over the characters in the string.
  • The character count is increased each time a character appears, whereas the word count is increased only when space appears.
  •  The total number of characters and words in the string is printed.

Method #2:Using Count Variable(User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Take a variable to say stringwords that stores the total words in the given string.
  • Initialize the stringwords to 1.
  • Take a variable to say stringchars that store the total characters in the given string.
  • Initialize the stringchars to 0.
  • To Traverse, the characters in the string, use a for loop and increment the stringchars variable each time by 1.
  • If a space character is encountered then increment the stringwords by 1.
  • Print the total number of characters and words in the string i.e stringchars and stringwords.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using input() function and store it in a variable.
given_string = input('Enter some random string =')
# Take a variable to say stringchars that store the total characters in the given string.
# Initialize the stringchars to 0.
stringchars = 0
# Take a variable to say stringwords that stores the total words in the given string.
# Initialize the stringwords to 1.
stringwords = 1
# To traverse the characters in the string, use a For loop.
for charact in given_string:
   # If a space character is encountered then increment the stringwords by 1.
    if(charact == ' '):
        stringwords = stringwords+1
     # increment the stringchars variable each time by 1.
    stringchars = stringchars+1
# Print the stringLength.
print(
    'Total characters present in given string {', given_string, '} =', stringchars)
print(
    'Total words present in given string {', given_string, '} =', stringwords)

Output:

Enter some random string =hello this is btechgeeks online platform for coding students 
Total characters present in given string { hello this is btechgeeks online platform for coding students } = 61
Total words present in given string { hello this is btechgeeks online platform for coding students } = 9

The output is the total number of characters and words present in the given string.

Method #3:Using split() and len() functions(Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Split the given string into a list of words using list() and split() functions.
  • The length of the list gives the total number of words in the given string and store it in a variable say stringwords.
  • The length of the given string is calculated using the len() function which gives the total number of characters present in the given string.
  • Store this characters count in a variable say stringchars.
  • Print the total number of characters and words in the string i.e stringchars and stringwords.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
given_string = 'hello this is btechgeeks'
# Split the given string into a list of words using list() and split() functions.
listwords = given_string.split()
# The length of the list gives the total number of words in the given string and
# store it in a variable say stringwords.
stringwords = len(listwords)
# The length of the given string is calculated using the len() function which
# gives the total number of characters present in the given string.
stringLeng = len(given_string)
# Store this characters count in a variable say stringchars.
stringchars = stringLeng
#Print the total words and character present in the given string
print('Total characters present in given string {', given_string, '} =', stringchars)
print('Total words present in given string {', given_string, '} =', stringwords)

Output:

Total characters present in given string { hello this is btechgeeks } = 24
Total words present in given string { hello this is btechgeeks } = 4

Method #4:Using split() and len() functions(User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Split the given string into a list of words using list() and split() functions.
  • The length of the list gives the total number of words in the given string and store it in a variable say stringwords.
  • The length of the given string is calculated using the len() function which gives the total number of characters present in the given string.
  • Store this characters count in a variable say stringchars.
  • Print the total number of characters and words in the string i.e stringchars and stringwords.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using input() function and store it in a variable.
given_string = input('Enter some random string = ')
# Split the given string into a list of words using list() and split() functions.
listwords = given_string.split()
# The length of the list gives the total number of words in the given string and
# store it in a variable say stringwords.
stringwords = len(listwords)
# The length of the given string is calculated using the len() function which
# gives the total number of characters present in the given string.
stringLeng = len(given_string)
# Store this characters count in a variable say stringchars.
stringchars = stringLeng
# Print the total words and character present in the given string
print('Total characters present in given string {', given_string, '} =', stringchars)
print('Total words present in given string {', given_string, '} =', stringwords)

Output:

Enter some random string = hello this is btechgeeks online platform for coding students
Total characters present in given string { hello this is btechgeeks online platform for coding students } = 61
Total words present in given string { hello this is btechgeeks online platform for coding students } = 9

Related Programs: