Pythonic way to create a long multi-line string

后端 未结 27 1706
滥情空心
滥情空心 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 01:15

    For example:

    sql = ("select field1, field2, field3, field4 "
           "from table "
           "where condition1={} "
           "and condition2={}").format(1, 2)
    
    Output: 'select field1, field2, field3, field4 from table 
             where condition1=1 and condition2=2'
    

    if the value of condition should be a string, you can do like this:

    sql = ("select field1, field2, field3, field4 "
           "from table "
           "where condition1='{0}' "
           "and condition2='{1}'").format('2016-10-12', '2017-10-12')
    
    Output: "select field1, field2, field3, field4 from table where
             condition1='2016-10-12' and condition2='2017-10-12'"
    

提交回复
热议问题