Page 2 of 2
Re: Multiline f strings in v1.17 - syntax error
Posted: Wed Sep 15, 2021 10:01 pm
by bulletmark
pythoncoder wrote: ↑Wed Sep 15, 2021 7:15 am
@jimmo I was led to believe that string
join is better than
+ from the point of view of allocation. Is this no longer the case?
That's true when you are appending in a loop but that's not the case here.
General Python programming advice is that rather than repeatedly append to another string in a loop you should append to a list and join that list at the end. However, here we are talking about a single join so I would say the "+" is clearer and just as, if not more so, efficient.
Re: Multiline f strings in v1.17 - syntax error
Posted: Thu Sep 16, 2021 7:10 am
by stijn
You cannot use + to concatenate over multiple lines though (without specifying a line continuation \ like jimmo shows but then you don't really need a + in the first place), which is specifically what the OP was asking
Re: Multiline f strings in v1.17 - syntax error
Posted: Thu Sep 16, 2021 7:31 am
by chrisb2
Based on the feedback I have updated my code to:
Code: Select all
def timestamp():
"""Get current timezone date/time from RTC as DD-MM-YYYY HH24:MM:SS."""
dt = datetime()
return f"{dt.day:02d}-{dt.month:02d}-{dt.year:d} " +\
f"{dt.hour:02d}:{dt.minute:02d}:{dt.second:02d}"