What if we want to print today’s date in the following format? mm/dd/yyyy. Let’s use string substitution again!

from datetime import datetime now = datetime.now() print '%02d-%02d-%04d' % (now.month, now.day, now.year) # will print the current date as mm-dd-yyyy

Remember that the standalone % operator after the string will fill the %02d and %04d placeholders in the string on the left with the numbers and strings in the parentheses on the right.

%02d pads the month and day numbers with zeros to two places, and %04d pads the year to four places. This is one way dates are commonly displayed.



Print the current date in the form of mm/dd/yyyy.

Change the string used above so that it uses a / character in between the %02d and %04d placeholders instead of a - character.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?