Multiline f strings in v1.17 - syntax error

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
bulletmark
Posts: 59
Joined: Mon Mar 29, 2021 1:36 am
Location: Brisbane Australia

Re: Multiline f strings in v1.17 - syntax error

Post by bulletmark » Wed Sep 15, 2021 10:01 pm

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.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Multiline f strings in v1.17 - syntax error

Post by stijn » Thu Sep 16, 2021 7:10 am

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

chrisb2
Posts: 28
Joined: Sat Apr 01, 2017 4:19 am

Re: Multiline f strings in v1.17 - syntax error

Post by chrisb2 » Thu Sep 16, 2021 7:31 am

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}"

Post Reply