In Python, if you need to repeatedly append to a string, you should convert it to a list, append your items to that list, and then join the list back into a string after you've made all the additions.
To illustrate, imagine you are reading through a long log file looking for lines that begin with "ERROR:" You want to store all those lines in a string and write them out to a new file.
Your first inclination might be to do that like this:
with open('log.txt') as f: log = f.readlines() new_log = '' for line in log: if line[:6] == 'ERROR:': new_log += line with open('newlog.txt','w') as f: f.write(new_log)
But as Python strings are immutable, that method requires creating new memory allocation each time you modify the string.
Instead, you should do the following:
with open('log.txt') as f: log = f.readlines()
new_log = 
loglist looking for matches and append each matching line to the
for line in log: if line[:6] == 'ERROR:': new_log.append(line)
new_logon an empty string and write it to the new log file:
with open('newlog.txt','w') as f: f.write(''.join(new_log))
If you're only making a few changes to the string, you won't notice much difference, but if you're making thousands, this method is much more efficient.
Nat Dunn founded Webucator in 2003 to combine his passion for technical training with his business expertise and to help companies benefit from both. His previous experience was in sales, business and technical training, and management. Nat has an MBA from Harvard Business School and a BA in International Relations from Pomona College.