Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1 (String slicing) Write a function called date that takes a single stri

ID: 3932748 • Letter: P

Question

Problem 1 (String slicing) Write a function called date that takes a single string as input which represents a date. The input string will always look like "dd/mm/yyyy", where dd is the day, mm is the month and yyyy is the year. The function then prints out the date in three (3) different formats: format 1: 'mm/dd/yyyy format 2: yyyy/mm/dd' format 3: 'dd-mm-yy The last format uses dashes instead of slashes to separate the numbers and only uses the last 2 numbers from the date For example, calling date("27/02/2003") will print to the screen 02/27/2003 2003/02/27 27-02-03 You do not need to convert any part of the input string into a number. The input string is always a fixed length and has the same structure. Use string slicing to solve this.

Explanation / Answer

import datetime

datestr = raw_input("Enter date string: ")
format = "%d/%m/%Y"

#convert to date object
date = datetime.datetime.strptime(datestr,format).date()

#format 1
print str(date.month).zfill(2)+"/"+str(date.day)+"/"+str(date.year)

#format 2
print str(date.year)+"/"+str(date.month).zfill(2)+"/"+str(date.day)

#format 3
print str(date.day)+"-"+str(date.month).zfill(2)+"-"+str(date.year%100).zfill(2)

import re

#use regular expression

def number_of_hits(text, substring):
   return len(re.findall('(?={0})'.format(re.escape(substring)), text))

text = raw_input("Enter main string: ")
substring = raw_input("Enter sub-string: ")
count = number_of_hits(text,substring)
print count

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote