Python Forum

Full Version: best way to build as string from pieces
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.