Bug or feature: double strings as one

MRAB python at mrabarnett.plus.com
Sun Aug 9 19:51:48 EDT 2009


Jan Kaliszewski wrote:
> 09-08-2009 o 23:43:14 r <rt8396 at gmail.com> wrote:
> 
>> #-- el bueno --#
>> "hello i am a very long string that\
>> does not like newlines so please \
>> escape me, Thank you!"
> 
> You probably ment: """hello i am... [etc.]
> 
> Anyway... You're right that generally it's good idea to define
> dialog prompts and such stuff separately rather that to hardcode
> it, especially in big projects. Then certainly multiline
> string literals are useful (though, if you need to get rid of
> newlines, IMHO "el feo" method is more elegant and less error
> prone than your "el bueno" [possible invisible space after '\']).
> 
> But there are plenty of other situations when it's better
> (in practice) to have strings (even long) together with your
> code, e.g. log information for debugging, or
> 
Here's an idea that you're probably going to hate: indented strings. :-)

A string prefixed with 'i' would be an 'indented' string. Leading space
and tab characters at the start of the string (just after the quote) or
the start of each line within a multiline string would be ignored.

 >>> """Line 1
        Line 2
"""
'Line 1\n       Line 2\n'
 >>> i"""Line 1
        Line 2
"""
'Line 1\nLine 2\n'

An additional feature could be to make '\ ' == ' ', perhaps only for
indented strings if you're worried that it could breaking existing code:

 >>> i"""Line 1
\       Line 2
"""
'Line 1\n       Line 2\n'

This would save you having to write '\x20'.



More information about the Python-list mailing list