Pythonic way to create a long multi-line string

后端 未结 27 1998
滥情空心
滥情空心 2020-11-22 00:47

I have a very long query. I would like to split it in several lines in Python. A way to do it in JavaScript would be using several sentences and joining them with a +<

27条回答
  •  醉梦人生
    2020-11-22 00:58

    I find that when building long strings, you are usually doing something like building an SQL query, in which case this is best:

    query = ' '.join((  # note double parens, join() takes an iterable
        "SELECT foo",
        "FROM bar",
        "WHERE baz",
    ))
    

    What Levon suggested is good, but might be vulnerable to mistakes:

    query = (
        "SELECT foo"
        "FROM bar"
        "WHERE baz"
    )
    
    query == "SELECT fooFROM barWHERE baz"  # probably not what you want
    

提交回复
热议问题