Python Programming - Calculate the Number of Digits and Letters in a String - Updated 2021

preview_player
Показать описание
In this video you will will learn how to Calculate the Number of Digits and Letters in a String (See the source further down)

Python Program to Calculate the Number of Digits and Letters in a String

==Source Code==

string=input("Enter string:")
count1=0
count2=0
for i in string:
count1=count1+1
count2=count2+1
print("The number of digits is:")
print(count1)
print("The number of characters is:")
print(count2)

==EndCode==

Program Explanation

1. User must enter a string and store it in a variable.
2. Both the count variables are initialized to zero.
3. The for loop is used to traverse through the characters in the string.
4. The first count variable is incremented each time a digit is encountered and the second count variable is incremented each time a character is encountered.
5. The total count of digits and characters in the string are printed.
Рекомендации по теме
Комментарии
Автор

thank you ...very useful, for my preparation

krish
Автор

message = 'Process finished with exit code 0'
count = {}

for character in message:
count.setdefault(character, 0)
count[character] += 1


print(count)

yeswanth.gontina
Автор

Your code shows digits and numbers but it also #Includes_Spaces.

nabeelmalik.
Автор

How we should calculate only characters

aspirant
Автор

letter ke saath saath space bhi count ho gyi.

rakshitjain
Автор

Didn't you say digits not characters

shakibhossain
Автор

how can we calculate different letters in a list of strings

zainabmursaleen