Python Forum

Full Version: What is this formatting called?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here's some brief code from an online Python exercise:

exam_st_date = (11,12,2014)
print( "The examination will start from : %i / %i / %i"%exam_st_date)
What is this type of formatting called? I want to learn more about it as I couldn't come up with any other way to extract and/or convert the specified date tuple into mm/dd/yyyy format (i.e. using datetime methods). Thanks!
Never mind. I googled those exact same two lines of code and found a discussion on another board that answers my question. Sorry for the trouble.
It's called string formatting and the oldest way with eg %i %s,should not be used anymore.
In Python 3.6 we got f-string,that is what you should use now.
from datetime import datetime, date

exam_st_date = datetime(2014,11,12)
print(f'The examination will start from: {exam_st_date:%m/%d/%Y}')
print(f'The examination will start from: {date.today():%m/%d/%Y}')
Output:
The examination will start from: 11/12/2014 The examination will start from: 12/14/2020