Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
use of format function
#1
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)
 
Reply
#2
Like thiis:
import datetime
current_time = datetime.datetime.now()
s = current_time.month
print(f'{s:02}')
jefsummers likes this post
Reply
#3
from datetime import datetime
print(datetime.now().strftime("%m"))
Reply
#4
More format codes
https://www.w3schools.com/python/gloss_p..._codes.asp
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  .format function Menthix 6 1,688 Mar-12-2022, 10:32 AM
Last Post: stevendaprano
  Date format and past date check function Turtle 5 4,284 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Money conversion - problems with lists and .format function fatherted99 1 1,825 Mar-12-2020, 06:29 PM
Last Post: ndc85430
  misunderstanding of format in print function Harvey 2 2,190 Oct-29-2019, 12:44 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020