Strings in Python:
“String is a character collection or array”
Well in Python too, for the string data type, we say the same definition. The string is a sequenced character array and is written within single, double, or three quotes. Also, Python does not have the data type character, thus it is used as a string of length 1 if we write ‘r’.
Given a string, the task is to calculate the total number of digits and letters present in the given string in Python.
Examples:
Example1:
Input:
given string =Hel34lo18th3is9is38 BTech23Geeks
Output:
The total number of digits present in the given string [ Hel34lo18th3is9is38 BTech23Geeks ] = 10 The total number of characters present in the given string [ Hel34lo18th3is9is38 BTech23Geeks ] = 32
- Python Program to Check Armstrong Number
- Python Program to Reverse a String Using Recursion
- 5 Simple Ways to Add a Newline Character in Python
Example2:
Input:
given string =btechgeeks2online82platform92for1000geeks
Output:
Enter some random string = btechgeeks2online82platform92for1000geeks The total number of digits present in the given string [ btechgeeks2online82platform92for1000geeks ] = 9 The total number of characters present in the given string [ btechgeeks2online82platform92for1000geeks ] = 41
Program to Calculate the Number of Digits and Letters in a String
There are several ways to calculate the total number of digits and letters in the given string some of them are:
- Using isdigit() function and Count Variable(Static Input)
- Using isdigit() function and Count Variable(User Input)
- Using isdigit() and len() functions(Static Input)
- Using isdigit() and len() functions(User Input)
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language
Method #1:Using isdigit() function and Count Variable(Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Take a variable to say stringdigits that stores the total digits in the given string.
- Initialize the stringdigits to 0.
- Take a variable to say stringcharacters that stores the total characters in the given string.
- Initialize the stringcharacters to 0.
- Traverse the given string using for loop.
- Check if the character is a numerical digit or not using the isdigit() function.
- If the character is a numerical digit then increment the value of stringdigits by 1.
- Increase the stringcharacters by 1.
- Print the total count of digits and characters present in the given string.
- The Exit of the Program
Below is the implementation:
# Give the string as static input and save it in a variable. given_strng = 'Hel34lo18th3is9is38 BTech23Geeks' # Take a variable to say stringdigits that stores the total digits in the given string. # Initialize the stringdigits to 0. stringdigits = 0 # Take a variable to say stringcharacters that stores the total characters in the given string. # Initialize the stringcharacters to 0. stringcharacters = 0 # Traverse the given string using for loop. for charact in given_strng: # Check if the character is a numerical digit or not using the isdigit() function. if(charact.isdigit()): # If the character is a numerical digit then increment the value of stringdigits by 1. stringdigits = stringdigits+1 # Increase the stringcharacters by 1. stringcharacters = stringcharacters+1 # Print the total count of digits and characters present in the given string. print( 'The total number of digits present in the given string [', given_strng, '] = ', stringdigits) print( 'The total number of characters present in the given string [', given_strng, '] = ', stringcharacters)
Output:
The total number of digits present in the given string [ Hel34lo18th3is9is38 BTech23Geeks ] = 10 The total number of characters present in the given string [ Hel34lo18th3is9is38 BTech23Geeks ] = 32
Method #2:Using isdigit() function and Count Variable(User Input)
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Take a variable to say stringdigits that stores the total digits in the given string.
- Initialize the stringdigits to 0.
- Take a variable to say stringcharacters that stores the total characters in the given string.
- Initialize the stringcharacters to 0.
- Traverse the given string using for loop.
- Check if the character is a numerical digit or not using the isdigit() function.
- If the character is a numerical digit then increment the value of stringdigits by 1.
- Increase the stringcharacters by 1.
- Print the total count of digits and characters present in the given string.
- The Exit of the Program
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable. given_strng = input('Enter some random string = ') # Take a variable to say stringdigits that stores the total digits in the given string. # Initialize the stringdigits to 0. stringdigits = 0 # Take a variable to say stringcharacters that stores the total characters in the given string. # Initialize the stringcharacters to 0. stringcharacters = 0 # Traverse the given string using for loop. for charact in given_strng: # Check if the character is a numerical digit or not using the isdigit() function. if(charact.isdigit()): # If the character is a numerical digit then increment the value of stringdigits by 1. stringdigits = stringdigits+1 # Increase the stringcharacters by 1. stringcharacters = stringcharacters+1 # Print the total count of digits and characters present in the given string. print( 'The total number of digits present in the given string [', given_strng, '] = ', stringdigits) print( 'The total number of characters present in the given string [', given_strng, '] = ', stringcharacters)
Output:
Enter some random string = btechgeeks2online82platform92for1000geeks The total number of digits present in the given string [ btechgeeks2online82platform92for1000geeks ] = 9 The total number of characters present in the given string [ btechgeeks2online82platform92for1000geeks ] = 41
Explanation:
- A string must be entered by the user and saved in a variable.
- The count variables are both set to zero.
- The for loop is used to go over the characters in a string.
- When a digit is encountered, the first count variable is incremented, and when a character is encountered, the second count variable is incremented.
- The total number of digits and characters in the string is printed.
Method #3:Using isdigit() and len() functions(Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Take a variable to say stringdigits that stores the total digits in the given string.
- Initialize the stringdigits to 0.
- Traverse the given string using for loop.
- Check if the character is a numerical digit or not using the isdigit() function.
- If the character is a numerical digit then increment the value of stringdigits by 1.
- Calculate the length of the given string using the len() function.
- The length of a given string gives the total number of characters present in the given string.
- Print the total count of digits and characters present in the given string.
- The Exit of the Program
Below is the implementation:
# Give the string as static input and save it in a variable. given_strng = 'Hel34lo18th3is9is38 BTech23Geeks' # Take a variable to say stringdigits that stores the total digits in the given string. # Initialize the stringdigits to 0. stringdigits = 0 # Traverse the given string using for loop. for charact in given_strng: # Check if the character is a numerical digit or not using the isdigit() function. if(charact.isdigit()): # If the character is a numerical digit then increment the value of stringdigits by 1. stringdigits = stringdigits+1 # Calculate the length of the given string using the len() function. # The length of a given string gives the total # number of characters present in the given string. strngLength = len(given_strng) # Print the total count of digits and characters present in the given string. print( 'The total number of digits present in the given string [', given_strng, '] = ', stringdigits) print( 'The total number of characters present in the given string [', given_strng, '] = ', strngLength)
Output:
The total number of digits present in the given string [ Hel34lo18th3is9is38 BTech23Geeks ] = 10 The total number of characters present in the given string [ Hel34lo18th3is9is38 BTech23Geeks ] = 32
Method #4:Using isdigit() and len() functions(User Input)
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Take a variable to say stringdigits that stores the total digits in the given string.
- Initialize the stringdigits to 0.
- Traverse the given string using for loop.
- Check if the character is a numerical digit or not using the isdigit() function.
- If the character is a numerical digit then increment the value of stringdigits by 1.
- Calculate the length of the given string using the len() function.
- The length of a given string gives the total number of characters present in the given string.
- Print the total count of digits and characters present in the given string.
- The Exit of the Program
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable. given_strng = input('Enter some random string = ') # Take a variable to say stringdigits that stores the total digits in the given string. # Initialize the stringdigits to 0. stringdigits = 0 # Traverse the given string using for loop. for charact in given_strng: # Check if the character is a numerical digit or not using the isdigit() function. if(charact.isdigit()): # If the character is a numerical digit then increment the value of stringdigits by 1. stringdigits = stringdigits+1 # Calculate the length of the given string using the len() function. # The length of a given string gives the total # number of characters present in the given string. strngLength = len(given_strng) # Print the total count of digits and characters present in the given string. print( 'The total number of digits present in the given string [', given_strng, '] = ', stringdigits) print( 'The total number of characters present in the given string [', given_strng, '] = ', strngLength)
Output:
Enter some random string = btechgeeks278onlineplatform2391forgeeks The total number of digits present in the given string [ btechgeeks278onlineplatform2391forgeeks ] = 7 The total number of characters present in the given string [ btechgeeks278onlineplatform2391forgeeks ] = 39
Related Programs:
- Python Program to Count the Number of Digits Present In a Number
- Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
- Python Program to Form an Integer that has the Number of Digits at Ten’s Place and the Least Significant Digit of the Entered Integer at One’s Place
- Python Program to Find the Sum of the Digits of the Number Recursively
- Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10