string concatenation
Peter Otten
__peter__ at web.de
Mon Jun 28 02:43:17 EDT 2004
selwyn wrote:
> you can += strings, but you need to create the variable first:
>
> i.e.
> for name in contextForm.keys():
> context = ''
> context += "Input: " + name + " value: " + contextForm[name].value
> + "<BR>"
I guess the OP wants to collect data over the loop, i. e
context = ""
for name in contextForm.keys():
context += "Input: " + name + " value: " + contextForm[name].value
> concatenating a string like this is supposed to be substantially slower
> than using ''.join(sequence), where you can replace the blank string
> with the separator of your choice.
>
> using your example:
>
> for name in contextForm.keys():
> sequence = ["Input: ",name, " value: ", contextForm[name].value,
> "<BR>"]
> context = ' '.join(sequence)
The faster idiom will then become
sequence = []
for name in contextForm.keys():
sequence += ["Input: ", name, " value: ", contextForm[name].value,
"<BR>"]
context = "".join(sequence)
Peter
More information about the Python-list
mailing list