Fwd: string concatenate

Tommy Grav tgrav at mac.com
Wed Oct 1 18:50:25 CEST 2008


On Oct 1, 2008, at 12:41 PM, sandric ionut wrote:

> Hi:
>
> I have the following situation:
>    nameAll = []
>    for i in range(1,10,1):
>        n = "name" + str([i])
>        nameAll += n
>    print nameAll
>
> I get:
>
> ['n', 'a', 'm', 'e', '[', '1', ']', 'n', 'a', 'm', 'e', '[', '2',  
> ']', 'n', 'a', 'm', 'e', '[', '3', ']', 'n', 'a', 'm', 'e', '[',  
> '4', ']', 'n', 'a', 'm', 'e', '[', '5', ']', 'n', 'a', 'm', 'e',  
> '[', '6', ']', 'n', 'a', 'm', 'e', '[', '7', ']', 'n', 'a', 'm',  
> 'e', '[', '8', ']', 'n', 'a', 'm', 'e', '[', '9', ']']
>
> but I would like to have it as:
>
> name1 name2 name3 ...name10
>
> How can I do it?
>
> Thank you,
>
> Ionut

 > nameAll = []
 > for i in xrange(1,10,1):
 >    n = "name" + str(i)
 >    nameAll.append(n)
 > print nameAll
['name1', 'name2', 'name3', 'name4', 'name5', 'name6', 'name7',  
'name8', 'name9']

list.append() is the right tool for adding new elements to a list.

Cheers
  Tommy




More information about the Python-list mailing list