views:

76

answers:

2

I have a long string that I build with a bunch of calculated values. I then write this string to a file. I have it formatted like:

string = str(a/b)+\
         '\t'+str(c)\
         '\t'+str(d)\
         ...
         '\n' 

I would like to add comment to what each value represents but commenting with # or ''' doesn't work. Here's an example:

string = str(a/b)+\     #this value is something
         '\t'+str(c)\   #this value is another thing
         '\t'+str(d)\   #and this one too
         ...
         '\n'

I figured out it doesn't work :) so I'm wondering what code with a clean syntax would look like in a situation like this.

The only option that occurs to me would be to go for string += on each line but I'm scratching my head with "there must be a better way".

+6  A: 

A simple solution is to use parenthesis instead:

string = (str(a/b)+      #this value is something
          '\t'+str(c)+   #this value is another thing
          '\t'+str(d)+   #and this one too
          ...
          '\n')
sth
For a cleaner syntax, "+" can be replaced with ",", the "\t" removed, then finally join them using "\t" (the final \n should be added after this, though)
RichN
+1  A: 

How about

string = '\t'.join(map(str,((a/b),    #this value is something
                            c,        #this value is another thing
                            d,        #and this one too
                            )))+'\n'

Or if you prefer

string = '\t'.join(map(str,(
        (a/b),    #this value is something
        c,        #this value is another thing
        d,        #and this one too
        )))+'\n'
gnibbler