Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
Every string operation returns a new string, so repeated += in a loop can copy data many times; collect the parts in a list and use ''.join(parts), and use f-strings for formatting.
You cannot change a character of a string in place; methods like replace, upper and strip return new strings. Concatenating with += inside a loop can create a new string on each iteration, which becomes slow for large inputs, even though CPython sometimes optimizes it. Appending pieces to a list and calling ''.join(parts) once is linear and reliable. For formatting values, f-strings such as f'{name}: {total:.2f}' are the clearest and fastest option. For very large outputs, writing to a file or an io.StringIO avoids holding everything at once.