6 ms·
> There is a solution: parentheses without commas. I don't know why this works, but I'm glad it does. It's worth mentioning that this is a somewhat controversi
by ckuehl 12y ago
> There is a solution: parentheses without commas. I don't know why this works, but I'm glad it does.
It's worth mentioning that this is a somewhat controversial practice. Guido has even discussed removing C-style string literal concatenation:
http://lwn.net/Articles/551438/ http://lwn.net/Articles/551438/
You may wish to consult your project's style guide and linter settings before using it.
- smoe 12y agoI personally don't like this style of using multiple strings. Makes radical changes of the text cumbersome. I think in most cases it's better to use triple quotes. And if the content of these variables isn't exclusively shown in the shell, you should use translation files anyway.
- philsnow 12y ago$ cat triple.py def foo(): print """this is a triple quoted string this is a continuation of a triple quoted string""" if __name__ == '__main__': foo() $ python triple.py this is a triple quoted string this is a continuation of a triple quoted string This is really warty. In bash you can mostly get around this with <<- for here documents (which removes leading indentation, so even if you're defining a here doc at a place that itself is indented, you don't have to align the text to the left column yourself). The man page in my version suggests it only works for leading tabs, not spaces, though. e.g. $ function usage() { cat <<-END this is a here document that spans multiple lines and is indented END } $ usage this is a here document that spans multiple lines and is indented $
- Spiritus 12y agoUse `textwrap.dedent()`?
- mercurial 12y agoDidn't know about textwrap, but this does not strike me as particularly lightweight. You really want built-in syntax for something like this.
- noinsight 12y agoI normally just do this for multiline strings: s = "\n".join(["one","two","three"])
- philsnow 12y agoThis happens at run time whereas IIRC the string literal is crammed pre-joined into the ,pyc / .pyo file. The performance hit of doing this kind of thing really adds up in a larger app.
- stdbrouw 12y agoDedent is nice, but then you still have to deal with removing single newlines (e.g. for error messages) and removing leading and trailing spaces. Ultimately nothing more than `re.sub(r'[^\n]\n[^\n]', '', textwrap.dedent(s).strip())` but kind of annoying to have to throw this in your code all over the place.
- abecedarius 12y agoI'd normally write def foo(): print """\ this is a triple quoted string this is a continuation of a triple quoted string"""
- smoe 12y agoI would put all messages in variables at the top of the file or even in a separate one and print them in the appropriate places. Having multi line prints in functions add a lot of noise in my opinion. When i read code, i dont normally care about the content of messages being printed.
- hollerith 12y agoI don't have an opinion positive or negative on it, but since many other design decisions have already been mentioned, here is Haskell's design decision for multi-line string literals. It allows "tidy indenting", but like the first design decision, interacts badly with "reflowing / reformatting / filling". http://book.realworldhaskell.org/read/characters-strings-and-escaping-rules.html#id689881 http://book.realworldhaskell.org/read/characters-strings-and...
- _paulc 12y agofrom __future__ import print_function import textwrap t = """ Hello there This is aligned """ # Need strip to get rid of extra NLs print(textwrap.dedent(t).strip())