Python Forum
use of format function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: use of format function (/thread-36241.html)



use of format function - barryjo - Jan-31-2022

In the following code, s is returned as an integer
The output is 1
How do I convert the integer s into a string with two decimal places.
i.e. I would like to print the month as 01 not 1
I assume I need a statement such as..
s = format(???????) before the print statement,.

import datetime
current_time = datetime.datetime.now()
s = current_time.month
print(s)
 



RE: use of format function - BashBedlam - Jan-31-2022

Like thiis:
import datetime
current_time = datetime.datetime.now()
s = current_time.month
print(f'{s:02}')



RE: use of format function - deanhystad - Feb-01-2022

from datetime import datetime
print(datetime.now().strftime("%m"))



RE: use of format function - menator01 - Feb-01-2022

More format codes
https://www.w3schools.com/python/gloss_python_date_format_codes.asp