Write a few lines of Python code to test if a date in the formatmonth/day/year exists in a given text string. For the month, dayand year, you can assume each contains minimum of one digit. So1/1/1 qualifies as a valid date.
Solution
code:
string = input(“Enter the string:”)
date = input(“Enter the date:”)
date_numbers = date.split(“/”)
date_valid = True
try:
for i in date_numbers:
if not (int(i) >=1):
date_valid = False
break
except:
date_valid = False
if(len(date_numbers) != 3):
date_valid = False
if(date_valid):
if(string.find(date) != -1):
print(“%s is found in %s” %(date, string))
else:
print(“%s is not found in %s” %(date,string))
else:
print(“Date is of invalid format”)
codescreenshot:
OUTPUT:
date_format.py
OR
OR