string concatenation

selwyn selwyn at aotearoa.is.home.nz
Mon Jun 28 02:35:05 EDT 2004


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>"

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)

HTH

	




Ajay wrote:
> hi!
> 
> i am going through a for loop and want to add the strings together
> i am doing this currently
> 
> for name in contextForm.keys():
>     context += "Input: " + name + " value: " + contextForm[name].value +
> "<BR>"
> 
> context is meant to hold all the form values in the paper.
> however the code above doesn't work
> 
> being new to Python, i dont know whether you can do +=
> 
> can you?
> 
> cheers
> --
> Ajay Brar,
> CS Honours 2004
> Smart Internet Technology Research Group
> 
> 
> 
> 
> 
> ----------------------------------------------------------------
> This message was sent using IMP, the Internet Messaging Program.
> 



More information about the Python-list mailing list