2

Let's say i've declared three variables which are a date, how can I combine them into a new variable where i can print them in the correct 1/2/03 format by simply printing the new variable name.

month = 1
day = 2
year = 03

date = month, day, year  <<<<< What would this code have to be? 

print(date)

I know i could set the sep='/' argument in the print statement if i call all three variables individually, but this means i can't add addition text into the print statement without it also being separated by a /. therefore i need a single variable i can call.

  • Note that your month, day, and year variables should be strings (e.g. `year = '03'`; otherwise the leading zero will be dropped. – dimo414 Sep 17 '15 at 14:00
  • Welcome to Stack Overflow! Thank you for asking a good question: you showed us what you've tried so far, and explained what you were trying to do. While you're here, I suggest taking the [tour of the site](http://stackoverflow.com/tour). And don't forget to "**accept**" whichever answer you find most useful, so that others who find your question later will know which answer solved your problem. To accept an answer, click on the checkmark below the answer's score, and the checkmark will turn green. (Note that you can only accept one answer per question.) – rmunn Sep 17 '15 at 14:00
  • According to [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) `1/2/03` isn't the correct format. ;-) – Matthias Sep 17 '15 at 14:01
  • @dimo414 - Actually, it's better to use integers for months, days and years, because you can't do math on strings. You should only convert them to strings (with something like `str.format()`) when you're ready to print them out. – rmunn Sep 17 '15 at 14:01
  • You *definitely* should not treat dates as integers if you're intending to do math. Use the appropriate date libraries for date arithmetic. If OP just wants to print a numeric value with a leading zero, they should use strings. – dimo414 Sep 17 '15 at 14:03

4 Answers4

5

The .join() method does what you want (assuming the input is strings):

>>> '/'.join((month, day, year))
1/2/03

As does all of Python's formatting options, e.g.:

>>> '%s/%s/%s' % (month, day, year)
1/2/03

But date formatting (and working with dates in general) is tricky, and there are existing tools to do it "right", namely the datetime module, see date.strftime().

>>> date = datetime.date(2003, 1, 2)
>>> date.strftime('%m/%d/%y')
'01/02/03'
>>> date.strftime('%-m/%-d/%y')
'1/2/03'

Note the - before the m and the d to suppress leading zeros on the month and date.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • Good idea pointing him to `datetime`. I think he should also look into [`str.format`](https://docs.python.org/3/library/stdtypes.html#str.format), as that's something every Python beginner should learn about. – rmunn Sep 17 '15 at 13:56
  • Your `.join` method seems to be more magic than mine, because mine is complaining about "expected string, int found" (Trivially fixed with `'/'.join(map(str,(month,day,year)))`) – NightShadeQueen Sep 17 '15 at 13:57
  • You can't use ints to represent strings with leading zeros - see my comment on the question. – dimo414 Sep 17 '15 at 13:59
1

You can use the join method. You can also use a list comprehension to format the strings so they are each 2 digits wide.

>>> '/'.join('%02d' % i for i in [month, day, year])
'01/02/03'
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

You want to read about the str.format() method:

https://docs.python.org/3/library/stdtypes.html#str.format

Or if you're using Python 2:

https://docs.python.org/2/library/stdtypes.html#str.format

The join() function will also work in this case, but learning about str.format() will be more useful to you in the long run.

rmunn
  • 34,942
  • 10
  • 74
  • 105
0

The correct answer is: use the datetime module:

import datetime

month = 1
day = 2
year = 2003

date = datetime(year, month, day)
print(date)
print(date.strftime("%m/%d/%Y"))

# etc

Trying to handle dates as tuples is just a PITA, so don't waste your time.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118