Write Lines Python Code Test Date Format Month Day Year Exists Given Text String Month Day Q37093959

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:

date_format.py - D:/Desktop/cg/date_format.py (3.7.1) Filc: lclit Poriat Run Ofotios Wirmiw lcli» string - input (Enter the

OUTPUT:

Python 3.7.1 Shell Eile Edit Shell Debug Options Window Help Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.

date_format.py

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.