In this program, we will take a string as input and check if a string has a number or not.
Steps to solve the program
- Take a string as input.
- Create a count variable and assign its value equal to 0.
- Use for loop to iterate over each character of the string.
- If the character is a number add 1 to the count variable.
- Use isnumeric() to check for numbers.
- If in the end count is greater is 0 then the string has a number.
- Print the output.
#Input string
string = "python1"
count = 0
for char in string:
if char.isnumeric():
count += 1
#Checking for numbers
if count > 0:
print("Given string have a number")
else:
print("Given string does not have a number")
Output :
Given string have a number