[Tutor] Thanks for your review

Alan Gauld alan.gauld at yahoo.co.uk
Tue Mar 26 05:39:16 EDT 2024


On 25/03/2024 19:59, Jonathan Wilson via Tutor wrote:

> newDBFileName = today.strftime(.format(today.month, today.day)) + '  '
>                                    ^
> SyntaxError: invalid syntax
> 
> Can someone tell me what the invalid syntax is here for this line?

> The arrow is pointing to the .ahead of the format.

Python has already told you, it is the dot in front of format.
Or more specifically the lack of anything in front of the dot.

format is a method of the string class. It expects to be
attached to a string instance with placeholders into which
it places its arguments. However, your usage of it
is also strange since strftime does much the same
for datetime objects. It's not clear why you'd use
both together.

In fact looking at the strftime call it looks like format
should be a variable containing a format string to tell
strftime how to do its work.

To help further you'd need to show us more context and
explain what it is you are trying to do! It is not at
all clear from this single line snippet.

As I see it there are two possible solutions depending on what you are
trying to do:
1) Add a format string and closing paren to strftime and apply format()
to the result:

newDBFileName = today.strftime("{}{} ").format(today.month, today.day))

2) Replace .format with a strftime format string and remove the
month/day arguments.

newDBFileName = today.strftime("%m%d ")

Here is some actual code:
>>> d = dt.datetime(2020,6,18)
>>> d.strftime("{} {} ").format(d.day, d.month)
'18 6 '
>>> d.strftime("%d %m ")
'18 06 '

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos





More information about the Tutor mailing list