Check if string contains numbers python – Python Program to Check if a String Contains at least One Number

Check if string contains numbers python: 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 given string has at least one digit in it

Example2:

Input:

Given String = "good morning btechgeeks"

Output:

No, the above-given string doesn't have at least one digit in it

Program to Check if a String Contains at least One Number in Python

Below are the ways to check whether the given string contains at least one number in python:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Take a variable say k and initialize its value to 0.
  • Calculate the length of the given string using the len() function store it in another variable.
  • Loop from 0 to the length of the given string using the for loop.
  • Check if the character present at the iterator value of the given string string is greater than or equal to 0 and less than or equal to 9 using the if conditional statement.
  • If the statement is true, then increment the value of the above-initialized by 1.
  • Exit the loop.
  • Check if the value of k is greater than or equal to 1 using the if conditional statement.
  • If the statement is true, then print “Yes, the above-given string has at least one digit in it”.
  • Else print “No, the above-given string doesn’t have at least one digit in it”.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "Hello 123 this is BTechGeeks online Platform "
# Take a variable say k and initialize its value to 0.
k = 0
# Calculate the length of the given string using the len() function.
# store it in another variable.
str_lengt = len(gvn_str)
# Loop from 0 to the length of the given string using the for loop.
for itr in range(0, str_lengt):
  # Check if the character present at the iterator value of the given string string is
  # greater than or equal to 0 and less than or equal to 9 using the if
  # conditional statement.
    if gvn_str[itr] >= '0' and gvn_str[itr] <= '9':
        # If the statement is true, then increment the value of the above-initialized k by 1.
        # Exit the loop.
        k += 1
# Check if the value of k is greater than or equal to 1 using the if conditional statement.
if k >= 1:
    # If the statement is true, then print "Yes, the above-given string has at least one
    # digit in it".
    print("Yes, the above given string has at least one digit in it")
else:
    # Else print "No, the above-given string doesn't have at least one digit in it".
    print("No, the above-given string doesn't have at least one digit in it")

Output:

Yes, the above given string has at least one digit in it

Method #2: Using For loop (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Take a variable say k and initialize its value to 0.
  • Calculate the length of the given string using the len() function store it in another variable.
  • Loop from 0 to the length of the given string using the for loop.
  • Check if the character present at the iterator value of the given string string is greater than or equal to 0 and less than or equal to 9 using the if conditional statement.
  • If the statement is true, then increment the value of the above-initialized by 1.
  • Exit the loop.
  • Check if the value of k is greater than or equal to 1 using the if conditional statement.
  • If the statement is true, then print “Yes, the above-given string has at least one digit in it”.
  • Else print “No, the above-given string doesn’t have at least one digit in it”.
  • 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.
gvn_str = input("Enter some random String = ")
# Take a variable say k and initialize its value to 0.
k = 0
# Calculate the length of the given string using the len() function.
# store it in another variable.
str_lengt = len(gvn_str)
# Loop from 0 to the length of the given string using the for loop.
for itr in range(0, str_lengt):
  # Check if the character present at the iterator value of the given string string is
  # greater than or equal to 0 and less than or equal to 9 using the if
  # conditional statement.
    if gvn_str[itr] >= '0' and gvn_str[itr] <= '9':
        # If the statement is true, then increment the value of the above-initialized k by 1.
        # Exit the loop.
        k += 1
# Check if the value of k is greater than or equal to 1 using the if conditional statement.
if k >= 1:
    # If the statement is true, then print "Yes, the above-given string has at least one
    # digit in it".
    print("Yes, the above given string has at least one digit in it")
else:
    # Else print "No, the above-given string doesn't have at least one digit in it".
    print("No, the above-given string doesn't have at least one digit in it")

Output:

Enter some random String = good morning btechgeeks
No, the above-given string doesn't have at least one digit in it

Access the big list of Python Programming Code Examples with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.