7

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

2 Answers 2

7

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')
Sign up to request clarification or add additional context in comments.

1 Comment

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)
1

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.