style question

Claudio Grondi claudio.grondi at freenet.de
Mon Jun 26 17:25:28 CEST 2006


Hari Sekhon wrote:
> Is it better to do:
> 
> message = """This is line1.
> This is line2
> This is line3\n"""
> 
> or
> 
> message = "This is line1.\n
> message = message + "This is line2\n"
> message = message + "This is line3\n"
> 
> 
> Since the first method does not follow python's clean and easy looking 
> indentation structure but the second just looks crude and ugly anyway.
> 
> If I indent the first version so the text is lined up to match code 
> indentation then this comes out in the input and isn't aligned there.
> 
What about

   message = """
   This is line 1
   This is line 2
   This is line 3
   """

When it is necessary to skip first empty line):
   message = """
   This is line 1
   This is line 2
   This is line 3
   """[1:]


When necessary to skip first line _and_ indentation:
   message = """
   This is line 1
   This is line 2
   This is line 3
   """.replace('\n  ', '\n')[1:] # adjust here '\n  ' to indentation
   # ^-- gives 'This is line 1\nThis is line 2\nThis is line 3\n'

?

Claudio



More information about the Python-list mailing list