Let us suppose that we have a string and we have to calculate the total number of digits and letters present in the string.
For Example
Input −
s = “tutorialsP0int”
Output −
Letters: 13 Digits: 1
Explanation −
Total number of letters and digits present in the given string are 13 and 1.
Approach to Solve this Problem
To calculate the total number of letters and digits in the given string, we have to first iterate over the whole string. If we get an alphabet, then we increment the letter count; otherwise, if we extract a digit, then increment the digit count.
Take an input string.
While iterating over the whole string, if we find a digit, then increment the count of digits; otherwise, if we find a letter, then increment the count of letters.
Return the count of letters and digits as the output.
Example
str = "tutorialsP0int" digit=letter=0 for ch in str: if ch.isdigit[]: digit=digit+1 elif ch.isalpha[]: letter=letter+1 else: pass print["Letters:", letter] print["Digits:", digit]
Output
Running the above code will generate the output as follows −
Letters: 13 Digits: 1
Updated on 06-Mar-2021 08:57:30
- Related Questions & Answers
- Python Program to Calculate the Number of Words and the Number of Characters Present in a String
- C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String
- Golang Program to Count the Number of Digits in a Number
- Java program to calculate the power of a number
- Write a program in Python to count the number of digits in a given number N
- Python Program to Find the Sum of Digits in a Number without Recursion
- Calculate the Power of a Number in Java Program
- Java Program to count letters in a String
- Python Program to Calculate the Length of a String Without Using a Library Function
- C++ Program to Calculate Power of a Number
- C++ Program to Sum the digits of a given number
- Program to find the sum of all digits of given number in Python
- Python Program to calculate the area of a Tetrahedron
- PHP program to sum the digits in a number
- How to sort the letters in a string alphabetically in Python?
Ignoring anything else that may or may not be correct with your "revised code", the issue causing the error currently quoted in your question is caused by calling the "count" function with an undefined variable because your didn't quote the string.
count[thisisastring222]
looks for a variable called thisisastring222 to pass to the function called count. For this to work you would have to have defined the variable earlier [e.g. withthisisastring222 = "AStringWith2NumberInIt."
] then your function will do what you want with the contents of the value stored in the variable, not the name of the variable.count["thisisastring222"]
hardcodes the string "thisisastring222" into the call, meaning that the count function will work with the exact string passed to it.
To fix your call to your function, just add quotes around asdfkasdflasdfl222
changing count[asdfkasdflasdfl222]
to count["asdfkasdflasdfl222"]
.
As far as the actual question "How to count digits, letters, spaces for a string in Python", at a glance the rest of the "revised code"
looks OK except that the return line is not returning the same variables you've used in the rest of the code. To fix it without changing anything else in the code, change number
and word
to digit
and letters
, making return number,word,space,other
into return digit,letters,space,other
, or better yet return [digit, letters, space, other]
to match current behavior while also using better coding style and being explicit as to what type of value is returned [in this case, a tuple].
Write a Python program that accepts a string and calculate the number of digits and letters. Pictorial Presentation: Sample Solution: Python Code: Sample Output: Flowchart: The following tool visualize what the computer is doing step-by-step as
it executes the said program: Python Conditional: Exercise-14 with Solution
s = input["Input a string"]
d=l=0
for c in s:
if c.isdigit[]:
d=d+1
elif c.isalpha[]:
l=l+1
else:
pass
print["Letters", l]
print["Digits", d]
Input a string W3resource
Letters 9
Digits 1
Visualize Python code execution:
Python Code Editor:
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a Python program which accepts a sequence of comma separated 4 digit binary numbers as its input and print the numbers that are divisible by 5 in a comma separated
sequence.
Next: Write a Python program to check the validity of a password [input from users].
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Summing a sequence of numbers [calculating the sum of zero to ten with skips]:
>>> l = range[0,10,2] >>> sum[l] 20
- Exercises: Weekly Top 16 Most Popular Topics
- SQL Exercises, Practice, Solution - JOINS
- SQL Exercises, Practice, Solution - SUBQUERIES
- JavaScript basic - Exercises, Practice, Solution
- Java Array: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : Conditional Statement
- HR Database - SORT FILTER: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : String
- Python Data Types: Dictionary - Exercises, Practice, Solution
- Python Programming Puzzles - Exercises, Practice, Solution
- C++ Array: Exercises, Practice, Solution
- JavaScript conditional statements and loops - Exercises, Practice, Solution
- C# Sharp Basic Algorithm: Exercises, Practice, Solution
- Python Lambda - Exercises, Practice, Solution
- Python Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation