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
  • 39,665
  • 11
  • 104
  • 149
macson taylor
  • 171
  • 7
  • 21

3 Answers3

2
text = "{:02d}/{:02d}/{:d}".format(*date) 
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45
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,068
  • 1
  • 9
  • 13
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,302
  • 7
  • 5