Page Contents
In this tutorial we will write a program that should prompt the user to type some sentence and count the number of characters in Python .
Steps
Steps to write a program that should prompt the user to type some sentence and count the number of characters in Python is mentioned below –
- First we will take input from user using
input()
function - Then we will initialize a variable
counter
as0
, iterate every character of the input and increase counter by 1 in each iteration - Then we will print the
counter
variable .
Counting the number of characters of a given sentence in Python
# Taking input from user
user_input=input("Type some sentence :\n")
# initializing counter variable
counter =0
# counting the total character
for character in user_input:
counter+=1
# printing the character count
print(counter)
Type some sentence : Python is a popular programming language . It was invented in 1991 . 68
- In the above code first we have taken input from user using
input()
function - Then we have initialized a variable
counter
as value0
. - Then we have iterated the input and increase the counter by
1
in each iteration . - After that we have printed the
counter
. - After executing the code it has prompted us to type some sentence . After typing sentence whenever we press Enter key it will print the no of character in the output .
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .