Pythonic way to create a long multi-line string

后端 未结 27 1644
滥情空心
滥情空心 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
    
    0 讨论(0)
  • 2020-11-22 00:58

    I find textwrap.dedent the best for long strings as described here:

    def create_snippet():
        code_snippet = textwrap.dedent("""\
            int main(int argc, char* argv[]) {
                return 0;
            }
        """)
        do_something(code_snippet)
    
    0 讨论(0)
  • 2020-11-22 00:58

    I like this approach because it privileges reading. In cases where we have long strings there is no way! Depending on the level of indentation you are in and still limited to 80 characters per line... Well... No need to say anything else. In my view the python style guides are still very vague. I took the @Eero Aaltonen approach because it privileges reading and common sense. I understand that style guides should help us and not make our lives a mess. Thanks!

    class ClassName():
        def method_name():
            if condition_0:
                if condition_1:
                    if condition_2:
                        some_variable_0 =\
    """
    some_js_func_call(
        undefined, 
        {
            'some_attr_0': 'value_0', 
            'some_attr_1': 'value_1', 
            'some_attr_2': '""" + some_variable_1 + """'
        }, 
        undefined, 
        undefined, 
        true
    )
    """
    
    0 讨论(0)
  • 2020-11-22 01:00

    Hey try something like this hope it works, like in this format it will return you a continuous line like you have successfully enquired about this property`

    "message": f'you have successfully inquired about '
               f'{enquiring_property.title} Property owned by '
               f'{enquiring_property.client}'
    
    0 讨论(0)
  • 2020-11-22 01:00

    Use triple quotation marks. People often use these to create docstrings at the start of programs to explain their purpose and other information relevant to its creation. People also use these in functions to explain the purpose and application of functions. Example:

    '''
    Filename: practice.py
    File creator: me
    File purpose: explain triple quotes
    '''
    
    
    def example():
        """This prints a string that occupies multiple lines!!"""
        print("""
        This
        is 
        a multi-line
        string!
        """)
    
    0 讨论(0)
  • 2020-11-22 01:02

    You can also place the sql-statement in a seperate file action.sql and load it in the py file with

    with open('action.sql') as f:
       query = f.read()
    

    So the sql-statements will be separated from the python code. If there are parameters in the sql statement which needs to be filled from python, you can use string formating (like %s or {field})

    0 讨论(0)
提交回复
热议问题