Python Forum
best way to build as string from pieces - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: best way to build as string from pieces (/thread-20757.html)



best way to build as string from pieces - Skaperen - Aug-29-2019

i'm building a string from pieces being appended. this does not fit into formatting or f-strings because they are maybe added according to an if conditional, repeated in a loop, and/or modified by function calls. i have been using a lot of += operators to do this. now i am wondering if it might be be better to .append() these strings to a list and ''.join() them all together at the end. a typical run in my current project will involve a few million += operations.


RE: best way to build as string from pieces - stranac - Aug-29-2019

Shouldn't really make a difference when using a modern-ish python.
A few years ago, using += would result in quadratic complexity code, but nowadays python is smart enough to optimize this use case.