0

How to change date inside a string?

Hi. I have to write a program that takes a string as input and for example,change 99/02/24 in that sentence to 1399/02/24. Can you help me? Sample input: Prior to 99/8/2 this included stocks of propylene held at terminals. Sample output: Prior to 1399/8/2 this included stocks of propylene held at terminals.

14th Apr 2021, 12:20 PM
Marjaf
Marjaf - avatar
3 Réponses
+ 1
# Taking Sentence as input sen= input('enter the sentence: ') # Spliting it up into a list of words c= sen.split() for element in c: if '/' in element: # Checking it is in date format or not d_elem = element.split('/') # getting date , month & year form the element d, m, y= d_elem # assigning date , month & year form the date element n_d= input('Enter the new date: ') # getting new date n_elem= element.replace(d, n_d) #replacing new date to element n_sen= sen.replace(element, n_elem) #replacing new element to sentence print(n_sen)
14th Apr 2021, 5:42 PM
Priyanshu Kumar
Priyanshu Kumar - avatar
0
my_str = input() my_str = "Prior to 1399/8/2 this included stocks of propylene held at terminals." print(my_str) in fact, you will not care what the user enters. in the second line of code, you will replace the value with the one you need
14th Apr 2021, 12:38 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Hi! In the SoloLearn course Python Core there is a part about regular expressions. There you can read aboute how to find patterns in strings, that you can replace with another string. In Python, methods to handle regular expression are in the re module, which you have to import.
14th Apr 2021, 6:03 PM
Per Bratthammar
Per Bratthammar - avatar