Pythonic way to create a long multi-line string

后端 未结 27 1673
滥情空心
滥情空心 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:12

    "À la" Scala way (but I think is the most pythonic way as OQ demands):

    description = """
                | The intention of this module is to provide a method to 
                | pass meta information in markdown_ header files for 
                | using it in jinja_ templates. 
                | 
                | Also, to provide a method to use markdown files as jinja 
                | templates. Maybe you prefer to see the code than 
                | to install it.""".replace('\n            | \n','\n').replace('            | ',' ')
    

    If you want final str without jump lines, just put \n at the start of the first argument of the second replace:

    .replace('\n            | ',' ')`.
    

    Note: the white line between "...templates." and "Also, ..." requires a whitespace after the |.

提交回复
热议问题