What's about the old school %-formatter method? Is the RAM allocation the same?!?The best way to create dynamic strings is by means of the string format() method:
Code: Select all
var = "Temperature {:5.2f} Pressure {:06d}\n".format(temp, press)
create dynamic strings
create dynamic strings
https://docs.micropython.org/en/latest/ ... d.html#ram says:
Re: create dynamic strings
Probably yes. Both the format string and the generated target need RAM.
Re: create dynamic strings
Definitely yes. They work in the same way, by appending to a vstr which grows as the string is generated.
The reason that ''.format() is preferred over % is that not all ports of MicroPython have % enabled.
The reason that ''.format() is preferred over % is that not all ports of MicroPython have % enabled.
Re: create dynamic strings
Oh, interesting. Then it is probably best to prefer .format().
Re: create dynamic strings
What's about this:
I don't think this should be an unnecessary waste of RAM either, is it?
Code: Select all
print('Some Text:', variable)
Re: create dynamic strings
That's right, this doesn't require any allocations -- it just prints the strings directly.