How to Count Numbers of Words and Characters in String in Python


In this example we will show how to calculate the numbers of words and characters that appear in the string.

2. Codes

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

str = input("Please input the string: ")
chars = 0
word = 1
for i in str:
    chars += 1
    if i == " ":
        word += 1
print("The numbesr of characters is: ", chars)
print("The numbesr of letters is: ", word)

Output:

Please input the string: Hello World
The numbesr of characters is: 11
The numbesr of letters is: 2
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments