{"id":20319,"date":"2021-09-09T19:57:53","date_gmt":"2021-09-09T14:27:53","guid":{"rendered":"https:\/\/python-programs.com\/?p=20319"},"modified":"2021-11-22T18:36:23","modified_gmt":"2021-11-22T13:06:23","slug":"python-program-to-check-if-a-string-contains-at-least-one-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-a-string-contains-at-least-one-number\/","title":{"rendered":"Python Program to Check if a String Contains at least One Number"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the Shortest Word in a String<\/a><\/p>\n Given a string and the task is to check whether the given string contains at least one number in it.<\/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 Below are the ways to check whether the given string contains at least one number in python:<\/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 Access the big list of Python Programming Code Examples<\/a> with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.<\/p>\n In the previous article, we have discussed Python Program to Find the Shortest Word in a String Given a string and the task is to check whether the given string contains at least one number in it. Examples: Example1: Input: Given String = “Hello 123 this is BTechGeeks online Platform ” Output: Yes, the above …<\/p>\nGiven String = \"Hello 123 this is BTechGeeks online Platform \"<\/pre>\n
Yes, the above given string has at least one digit in it<\/pre>\n
Given String = \"good morning btechgeeks\"<\/pre>\n
No, the above-given string doesn't have at least one digit in it<\/pre>\n
Program to Check if a String Contains at least One Number in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"Hello 123 this is BTechGeeks online Platform \"\r\n# Take a variable say k and initialize its value to 0.\r\nk = 0\r\n# Calculate the length of the given string using the len() function.\r\n# store it in another variable.\r\nstr_lengt = len(gvn_str)\r\n# Loop from 0 to the length of the given string using the for loop.\r\nfor itr in range(0, str_lengt):\r\n # Check if the character present at the iterator value of the given string string is\r\n # greater than or equal to 0 and less than or equal to 9 using the if\r\n # conditional statement.\r\n if gvn_str[itr] >= '0' and gvn_str[itr] <= '9':\r\n # If the statement is true, then increment the value of the above-initialized k\u00a0by 1.\r\n # Exit the loop.\r\n k += 1\r\n# Check if the value of k is greater than or equal to 1 using the if conditional statement.\r\nif k >= 1:\r\n # If the statement is true, then print \"Yes, the above-given string has at least one\r\n # digit in it\".\r\n print(\"Yes, the above given string has at least one digit in it\")\r\nelse:\r\n # Else print\u00a0\"No, the above-given string doesn't have at least one digit in it\".\r\n print(\"No, the above-given string doesn't have at least one digit in it\")\r\n<\/pre>\n
Yes, the above given string has at least one digit in it<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and\r\n# store it in a variable.\r\ngvn_str = input(\"Enter some random String = \")\r\n# Take a variable say k and initialize its value to 0.\r\nk = 0\r\n# Calculate the length of the given string using the len() function.\r\n# store it in another variable.\r\nstr_lengt = len(gvn_str)\r\n# Loop from 0 to the length of the given string using the for loop.\r\nfor itr in range(0, str_lengt):\r\n # Check if the character present at the iterator value of the given string string is\r\n # greater than or equal to 0 and less than or equal to 9 using the if\r\n # conditional statement.\r\n if gvn_str[itr] >= '0' and gvn_str[itr] <= '9':\r\n # If the statement is true, then increment the value of the above-initialized k\u00a0by 1.\r\n # Exit the loop.\r\n k += 1\r\n# Check if the value of k is greater than or equal to 1 using the if conditional statement.\r\nif k >= 1:\r\n # If the statement is true, then print \"Yes, the above-given string has at least one\r\n # digit in it\".\r\n print(\"Yes, the above given string has at least one digit in it\")\r\nelse:\r\n # Else print\u00a0\"No, the above-given string doesn't have at least one digit in it\".\r\n print(\"No, the above-given string doesn't have at least one digit in it\")\r\n<\/pre>\n
Enter some random String = good morning btechgeeks\r\nNo, the above-given string doesn't have at least one digit in it<\/pre>\n
\n