Pythonic way to create a long multi-line string

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

    You can also concatenate variables in when using """ notation:

    foo = '1234'
    
    long_string = """fosdl a sdlfklaskdf as
    as df ajsdfj asdfa sld
    a sdf alsdfl alsdfl """ +  foo + """ aks
    asdkfkasdk fak"""
    

    EDIT: Found a better way, with named params and .format():

    body = """
    
    
    
    
        

    Lorem ipsum.

    Asdf:
    {name}
    """.format( link='http://www.asdf.com', name='Asdf', ) print(body)

提交回复
热议问题