0

Good evening. Today, I was writing a piece of code in Python. I have a string that is called date. It holds the following data:

date='05/04/2014'

So. I want to split this string into several substrings, each holding the day, month or year. These substrings will be called day, month and year, with the respective number in each string. How could I do this?

Also, I would like this method to work for any other date strings, such as:

02/07/2012
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555

1 Answers1

2

Simply use:

day,month,year = date.split('/')

Here you .split(..) the string on the slash (/) and you use sequence unpacking to store the first, second and third group in day, month and year respectively. Here date is the string that contains the date ('05/04/2014') and '/' is the split pattern.

>>> day,month,year = date.split('/')
>>> day
'05'
>>> month
'04'
>>> year
'2014'

Note that day, month and year are still strings (not integers).

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555