{"id":10286,"date":"2021-10-01T10:00:51","date_gmt":"2021-10-01T04:30:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=10286"},"modified":"2021-11-22T18:33:30","modified_gmt":"2021-11-22T13:03:30","slug":"python-program-to-calculate-the-number-of-digits-and-letters-in-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-calculate-the-number-of-digits-and-letters-in-a-string\/","title":{"rendered":"Python Program to Calculate the Number of Digits and Letters in a String"},"content":{"rendered":"
Strings in Python:<\/strong><\/p>\n \u201cString is a character collection or array\u201d<\/strong><\/p>\n 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 \u2018r’.<\/p>\n Given a string, the task is to calculate the total number of digits and letters present in the given string in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to calculate the total number of digits and letters in the given string some of them are:<\/p>\n Want to excel in java coding? Practice with these Java Programs examples with output<\/a> and write any kind of easy or difficult programs in the java language<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Strings in Python: \u201cString is a character collection or array\u201d 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 …<\/p>\ngiven string =Hel34lo18th3is9is38 BTech23Geeks<\/pre>\n
The total number of digits present in the given string [ Hel34lo18th3is9is38 BTech23Geeks ] = 10\r\nThe total number of characters present in the given string [ Hel34lo18th3is9is38 BTech23Geeks ] = 32<\/pre>\n
given string =btechgeeks2online82platform92for1000geeks<\/pre>\n
Enter some random string = btechgeeks2online82platform92for1000geeks\r\nThe total number of digits present in the given string [ btechgeeks2online82platform92for1000geeks ] = 9\r\nThe total number of characters present in the given string [ btechgeeks2online82platform92for1000geeks ] = 41<\/pre>\n
Program to Calculate the Number of Digits and Letters in a String<\/h2>\n
\n
Method #1:Using isdigit() function and Count Variable(Static Input)<\/h3>\n
\n
# Give the string as static input and save it in a variable.\r\ngiven_strng = 'Hel34lo18th3is9is38 BTech23Geeks'\r\n# Take a variable to say stringdigits that stores the total digits in the given string.\r\n# Initialize the stringdigits to 0.\r\nstringdigits = 0\r\n# Take a variable to say stringcharacters that stores the total characters in the given string.\r\n# Initialize the stringcharacters to 0.\r\nstringcharacters = 0\r\n# Traverse the given string using for loop.\r\nfor charact in given_strng:\r\n # Check if the character is a numerical digit or not using the isdigit() function.\r\n if(charact.isdigit()):\r\n # If the character is a numerical digit then increment the value of stringdigits by 1.\r\n stringdigits = stringdigits+1\r\n # Increase the stringcharacters by 1.\r\n stringcharacters = stringcharacters+1\r\n# Print the total count of digits and characters present in the given string.\r\nprint(\r\n 'The total number of digits present in the given string [', given_strng, '] = ', stringdigits)\r\nprint(\r\n 'The total number of characters present in the given string [', given_strng, '] = ', stringcharacters)\r\n<\/pre>\n
The total number of digits present in the given string [ Hel34lo18th3is9is38 BTech23Geeks ] = 10\r\nThe total number of characters present in the given string [ Hel34lo18th3is9is38 BTech23Geeks ] = 32<\/pre>\n
Method #2:Using isdigit() function and Count Variable(User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a\u00a0 variable.\r\ngiven_strng = input('Enter some random string = ')\r\n# Take a variable to say stringdigits that stores the total digits in the given string.\r\n# Initialize the stringdigits to 0.\r\nstringdigits = 0\r\n# Take a variable to say stringcharacters that stores the total characters in the given string.\r\n# Initialize the stringcharacters to 0.\r\nstringcharacters = 0\r\n# Traverse the given string using for loop.\r\nfor charact in given_strng:\r\n # Check if the character is a numerical digit or not using the isdigit() function.\r\n if(charact.isdigit()):\r\n # If the character is a numerical digit then increment the value of stringdigits by 1.\r\n stringdigits = stringdigits+1\r\n # Increase the stringcharacters by 1.\r\n stringcharacters = stringcharacters+1\r\n# Print the total count of digits and characters present in the given string.\r\nprint(\r\n 'The total number of digits present in the given string [', given_strng, '] = ', stringdigits)\r\nprint(\r\n 'The total number of characters present in the given string [', given_strng, '] = ', stringcharacters)\r\n<\/pre>\n
Enter some random string = btechgeeks2online82platform92for1000geeks\r\nThe total number of digits present in the given string [ btechgeeks2online82platform92for1000geeks ] = 9\r\nThe total number of characters present in the given string [ btechgeeks2online82platform92for1000geeks ] = 41<\/pre>\n
\n
Method #3:Using isdigit() and len() functions(Static Input)<\/h3>\n
\n
# Give the string as static input and save it in a variable.\r\ngiven_strng = 'Hel34lo18th3is9is38 BTech23Geeks'\r\n# Take a variable to say stringdigits that stores the total digits in the given string.\r\n# Initialize the stringdigits to 0.\r\nstringdigits = 0\r\n# Traverse the given string using for loop.\r\nfor charact in given_strng:\r\n # Check if the character is a numerical digit or not using the isdigit() function.\r\n if(charact.isdigit()):\r\n # If the character is a numerical digit then increment the value of stringdigits by 1.\r\n stringdigits = stringdigits+1\r\n# Calculate the length of the given string using the len() function.\r\n# The length of a given string gives the total\r\n# number of characters present in the given string.\r\nstrngLength = len(given_strng)\r\n# Print the total count of digits and characters present in the given string.\r\nprint(\r\n 'The total number of digits present in the given string [', given_strng, '] = ', stringdigits)\r\nprint(\r\n 'The total number of characters present in the given string [', given_strng, '] = ', strngLength)\r\n<\/pre>\n
The total number of digits present in the given string [ Hel34lo18th3is9is38 BTech23Geeks ] = 10\r\nThe total number of characters present in the given string [ Hel34lo18th3is9is38 BTech23Geeks ] = 32<\/pre>\n
Method #4:Using isdigit() and len() functions(User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a\u00a0 variable.\r\ngiven_strng = input('Enter some random string = ')\r\n# Take a variable to say stringdigits that stores the total digits in the given string.\r\n# Initialize the stringdigits to 0.\r\nstringdigits = 0\r\n# Traverse the given string using for loop.\r\nfor charact in given_strng:\r\n # Check if the character is a numerical digit or not using the isdigit() function.\r\n if(charact.isdigit()):\r\n # If the character is a numerical digit then increment the value of stringdigits by 1.\r\n stringdigits = stringdigits+1\r\n# Calculate the length of the given string using the len() function.\r\n# The length of a given string gives the total\r\n# number of characters present in the given string.\r\nstrngLength = len(given_strng)\r\n# Print the total count of digits and characters present in the given string.\r\nprint(\r\n 'The total number of digits present in the given string [', given_strng, '] = ', stringdigits)\r\nprint(\r\n 'The total number of characters present in the given string [', given_strng, '] = ', strngLength)\r\n<\/pre>\n
Enter some random string = btechgeeks278onlineplatform2391forgeeks\r\nThe total number of digits present in the given string [ btechgeeks278onlineplatform2391forgeeks ] = 7\r\nThe total number of characters present in the given string [ btechgeeks278onlineplatform2391forgeeks ] = 39<\/pre>\n
\n