.format Strings Explanation

Hello,

Could someone explain why these two snippets of code are NOT the same thing please? This is from Step 6 in the Working w/ Dates and Times in Python section.

I think in general this topic of .format() and the braces {} trips me up because i do not understand the substitution method of the variables we define for the {} placements. I’m specifically curious about why it worked when I did ‘the string’.format versus assigning the string to a variable and then attempting to work through that pathway. Thank you!

Correct Code:

broken_up = []
date_format = "%m/%d/%y %H:%M"
for items in listfile[1:]:
    app_start = items[2]
    items[2] = dt.strptime(app_start, date_format)
    broken_up.append(items[2])

for items in broken_up:
    month = items.month
    day = items.day
    year = items.year
    varstring = "The best {}, on the best {} in the best {}.".format(month, day, year)
    print(varstring)

Wrong Code:

broken_up = []
date_format = "%m/%d/%y %H:%M"
for items in listfile[1:]:
    app_start = items[2]
    items[2] = dt.strptime(app_start, date_format)
    broken_up.append(items[2])

for items in broken_up:
    month = items.month
    day = items.day
    year = items.year
    varstring = "The best {month}, on the best {day} in the best {year}."
    varstring.format(month, day, year)
    print(varstring)

Hi.

The second code does not work, since you must explicitly specify values for each parameter

Example

    month = items.month
    day = items.day
    year = items.year
    varstring = "The best {month}, on the best {day} in the best {year}."
    varstring.format(day=day, month=month, year=year)

Alternatively, you can specify the position for tuple elements by their position

    month = items.month
    day = items.day
    year = items.year
    varstring = "The best {1}, on the best {0} in the best {2}."
    varstring.format(day, month, year)

I specifically changed the order of elements inside format() to show that these cases will be handled correctly

But what you provided will work correctly with minimal changes for Python version 3.6+

Starting from 3.6, Python added the so-called f-strings. You can apply the same formatting using the following code

    month = items.month
    day = items.day
    year = items.year
    varstring = f"The best {month}, on the best {day} in the best {year}."
1 Like

@moriturus7
Does the above really work?
What i usually see when specifying integer positions in {} is that the arguments in format() do not have keywords. (but they can have)
If there are indeed keywords, then they have to be used in the {insert keyword} in the string part before format. (but can be left out of the string part too)
I expect this to be a IndexError: tuple index out of range
since in format, there were 0 positional arguments provided and 3 keyword arguments (day,month,year), so it can’t even do {0}, nevermind reaching {1}, {2}.

'{0} and {1}'.format(10,20,a=10,b=20)
Remove either 1st or 2nd argument of format to see what i mean.

1 Like

Yes, you are right, I Did an example from the previous one and did not see my mistake. I edited my message.

1 Like

Thanks folks. I went back and did it over. The explanation helped a bit.
**broken_up is a list i made where it only contains the appt_date column as datetime objects… **

object_date = broken_up[1]
day = object_date.day
month = object_date.month
year = object_date.year
stringy = "{month}/{day}/{year}"
stringy2 = stringy.format(month=month, day=day, year=year)
print(stringy2)