詳解Python3 定義一個跨越多行的字符串的多種方法
方法一:使用三引號
>>> str1 = '''Le vent se lève, il faut tenter de vivre. 起風(fēng)了,唯有努力生存。 (縱有疾風(fēng)起,人生不言棄。)''' >>> str1 'Le vent se lève, il faut tenter de vivre. \n起風(fēng)了,唯有努力生存。\n(縱有疾風(fēng)起,人生不言棄。)' >>> print(str1) Le vent se lève, il faut tenter de vivre. 起風(fēng)了,唯有努力生存。 (縱有疾風(fēng)起,人生不言棄。)
編輯的時候,引號挺對的,但是不知道為什么發(fā)布的時候,第一行的引號總是多了一些,其實應(yīng)該是下面這樣的:
此種情況適用于想要多行表示某一多行字符串,實質(zhì)上字符串是多行。
再舉一個例子
>>> """ <div class="AuthorInfo-content"> <div class="AuthorInfo-head"> <span class="UserLink AuthorInfo-name"> <div class="Popover"> <div id="Popover222-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover222-content"> 作者:<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"> 簽名:{2} </div> </div> </div> </div> <br/> """.format("https://stackoverflow.com/questions/45624449", "Using Python Variables in HTML in multiline Python string", "123")
再舉一個用 f-string 格式化的例子,參考 https://realpython.com/python-f-strings/
>>> """ <div class="AuthorInfo-content"> <div class="AuthorInfo-head"> <span class="UserLink AuthorInfo-name"> <div class="Popover"> <div id="Popover222-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover222-content"> 作者:<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"> 簽名:{2} </div> </div> </div> </div> <br/> """.format("https://stackoverflow.com/questions/45624449", "Using Python Variables in HTML in multiline Python string", "123")
下面的兩種方法主要適用于一個長字符串一行表示不下,多行表示更為美觀,實質(zhì)上字符串還是一行。
方法二:使用反斜杠
>>> 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'
方法三:使用小括號
>>> str3 = ('Le vent se lève, il faut tenter de vivre.' '起風(fēng)了,唯有努力生存。' '(縱有疾風(fēng)起,人生不言棄。)') >>> str3 'Le vent se lève, il faut tenter de vivre.起風(fēng)了,唯有努力生存。(縱有疾風(fēng)起,人生不言棄。)'
到此這篇關(guān)于詳解Python3 定義一個跨越多行的字符串的多種方法的文章就介紹到這了,更多相關(guān)Python3 跨越多行的字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中shutil模塊的常用文件操作函數(shù)用法示例
shutil模塊提供比OS模塊更強(qiáng)大的本地文件操作功能,包括文件的壓縮和解壓縮等,下面我們就來列舉Python中shutil模塊的常用文件操作函數(shù)用法示例:2016-07-07Pandas groupby apply agg 的區(qū)別 運(yùn)行自定義函數(shù)說明
這篇文章主要介紹了Pandas groupby apply agg 的區(qū)別 運(yùn)行自定義函數(shù)說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03NetWorkX使用方法及nx.draw()相關(guān)參數(shù)解讀
這篇文章主要介紹了NetWorkX使用方法及nx.draw()相關(guān)參數(shù)解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12python socket網(wǎng)絡(luò)編程步驟詳解(socket套接字使用)
這篇文章主要介紹了什么是套接字、PYTHON套接字模塊,提供一個簡單的python socket編程,大家參考使用2013-12-12