the program receives one line as input, which outputs the message "digit" (without quotes) if the string contains a digit. otherwise, print the message "there are no numbers" (without quotes). in python

main.py
import re

# get input string from user
input_str = input("Enter a string: ")
# regex pattern to match any digit in string
pattern = r"\d"

# check if string contains a digit
if re.search(pattern, input_str):
    print("digit")
else:
    print("there are no numbers")
268 chars
13 lines

Explanation:

  • We import re module which provides support for regular expressions.
  • We get the input string from the user using input() function.
  • We define the regex pattern r"\d" which will match any digit in the string.
  • We use re.search() function to check if the pattern matches the input string.
  • If the pattern matches, we print "digit". Otherwise, we print "there are no numbers".

gistlibby LogSnag