0
date = [2, 5, 2018]
text = "%s/%s/%s" % tuple(date)
print(text)

It gives result 2/5/2018.How to convert it like 02/05/2018

Aran-Fey
  • 35,525
  • 9
  • 94
  • 135
macson taylor
  • 161
  • 7
  • 21

3 Answers3

2
text = "{:02d}/{:02d}/{:d}".format(*date) 
AGN Gazer
  • 7,569
  • 2
  • 21
  • 43
0

Use str.zfill(2) to pad your date pieces with 2 leading zeros:

date = [2, 5, 2018]
text = "%s/%s/%s" % tuple([str(date_part).zfill(2) for date_part in date])
print(text) # Outputs: 02/05/2018
Pierre
  • 1,038
  • 1
  • 8
  • 11
0
date = [2, 5, 2018]
text = "{:0>2}/{:0>2}/{}".format(*date)
print(text)

to learn more about using format, read: https://pyformat.info/

Sebastian Loehner
  • 1,165
  • 6
  • 5