On Thu, Feb 12, 2009 at 21:06, Andres Sanchez <telmo_and...@yahoo.com> wrote: > Please, give me a hand with this. Say I have a vector defined as > x=[1,2,3,4,...,9]. I need to write string variables with the names > G1BF...G9BF. Thus, the first string variable would be 'G'+'1'+'BF', being > the number 1 equal to x[0]. Now, using a for loop, I want to do something > like: y[i]='G'+x[i-1]+'BF' , to create my string variables. Yes, the problem > is that I am mixing numerical and string variables and Python reports an > error when I try to do this. So, the question is: how do I do to convert the > numerical variable x[0]=1 into a string variable '1', so I can do my > 'G'+'1'+BF' thing?
>>> x = 100 >>> x 100 >>> str(x) '100' You can use str() to convert the numbers to strings. To print what you are looking for do something like this. >>> x = [x for x in range(1,10)] >>> x [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for var in x: print 'G' + str(var) + 'BF' G1BF G2BF G3BF G4BF G5BF G6BF G7BF G8BF G9BF Or: >>> for var in x: print 'G%sBF' % var G1BF G2BF G3BF G4BF G5BF G6BF G7BF G8BF G9BF > By the way, 'G'+'x[0]'+'BF' reports 'Gx[0]BF' :) Correct, 'x[0]' is a string not the number you are looking for. See above on how you can do this. Greets Sander PS: personally I like the second one better :-) _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor