Method 1: Use triple quotes
>>> str1 = '''Le vent se lève, il faut tenter de vivre. When the wind rises, the only thing to do is to try to survive. (Even if the wind picks up, life is not forsaken.)''' >>> str1 'Le vent se lève, il faut tenter de vivre. \nIt's getting windy.,The only way to survive is to try.。\n(lit. even if there is a swift wind,Life is not about giving up.。)' >>> print(str1) Le vent se lève, il faut tenter de vivre. It's getting windy.,The only way to survive is to try.。 (lit. even if there is a swift wind,Life is not about giving up.。)
When editing, the quotes are pretty much correct, but for some reason when posting, there are always more quotes in the first line, which should actually look like the following:
This situation applies when you want a multi-line representation of a particular multi-line string, essentially the string is multi-line.
One more example.
>>> """ <div class="AuthorInfo-content"> <div class="AuthorInfo-head"> <span class="UserLink AuthorInfo-name"> <div class="Popover"> <div aria-haspopup="true" aria-expanded="false" aria-owns="Popover222-content"> author:<a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="{0}" rel="external nofollow" rel="external nofollow" >{1}</a> </div> </div> </span> </div> <div class="AuthorInfo-detail"> <div class="AuthorInfo-badge"> <div class="AuthorInfo-badgeText"> sign (one's name with a pen etc):{2} </div> </div> </div> </div> <br/> """.format("/questions/45624449", "Using Python Variables in HTML in multiline Python string", "123")
To give another example of formatting with f-string, refer to the/python-f-strings/
>>> """ <div class="AuthorInfo-content"> <div class="AuthorInfo-head"> <span class="UserLink AuthorInfo-name"> <div class="Popover"> <div aria-haspopup="true" aria-expanded="false" aria-owns="Popover222-content"> author:<a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="{0}" rel="external nofollow" rel="external nofollow" >{1}</a> </div> </div> </span> </div> <div class="AuthorInfo-detail"> <div class="AuthorInfo-badge"> <div class="AuthorInfo-badgeText"> sign (one's name with a pen etc):{2} </div> </div> </div> </div> <br/> """.format("/questions/45624449", "Using Python Variables in HTML in multiline Python string", "123")
The following two methods are mainly applicable to a long string of one line does not represent, multi-line representation is more beautiful, the essence of the string is still a line.
Method 2: Use backslashes
>>> name = "Eric" >>> profession = "comedian" >>> affiliation = "Monty Python" >>> message = f""" ... Hi {name}. ... You are a {profession}. ... You were in {affiliation}. ... """ ... >>> message '\n Hi Eric.\n You are a comedian.\n You were in Monty Python.\n'
Method 3: Use of parentheses
>>> str3 = ('Le vent se lève, il faut tenter de vivre.' 'The wind is picking up and the only way to survive is to try and survive.' '(Even if a stormy wind rises, life is not forsaken.)'') >>> str3 'Le vent se lève, il faut tenter de vivre. When the wind rises, the only thing to do is to try to survive. (Even if the wind picks up, life is not forsaken.)'
To this point this article on Python3 define a string across multiple lines of multiple methods of the article is introduced to this, more related to Python3 strings across multiple lines of content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!