How to Count Digits and Letters in a String in Python
In this tutorial, we will learn how to program "How to Count Digits and Letters in a String in Python." The objective is to efficiently count all the digits and letters present in a given string. This tutorial will guide you step by step through the entire process, demonstrating how to distinguish between letters and digits within a string. By the end of this tutorial, you will have a clear understanding of how to implement this task in Python, helping you strengthen your problem-solving abilities and coding skills.
This topic is straightforward to understand. Just follow the instructions provided, and you will be able to complete it with ease. The program demonstrated will guide you step by step through the process of counting the digits and letters in a string. So, let’s dive into the coding process!
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- while True:
- print("\n============== Count Digits and Letters in a String ==============\n")
- string=input("Enter string: ")
- count1=0
- count2=0
- for i in string:
- if(i.isdigit()):
- count1=count1+1
- count2=count2+1
- print("The number of digits is:")
- print(count1)
- print("The number of characters is:")
- print(count2)
- opt = input("\nDo you want to try again?(yes/no): ")
- if opt.lower() == 'yes':
- ret=False
- elif opt.lower() == 'no':
- ret=True
- print("Exiting program....")
- else:
- print("Please enter yes/no:")
- break
- if ret == False:
- continue
This Python program repeatedly asks the user to input a string, then counts and displays the number of digits and the total number of characters in the string, and allows the user to repeat or exit the process.
Output:

There you have it we successfully created How to Count Digits and Letters in a String in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language