位置:首頁(yè) > 軟件操作教程 > 編程開(kāi)發(fā) > Python > 問(wèn)題詳情

python應(yīng)用操作——多行字符串

提問(wèn)人:ylm發(fā)布時(shí)間:2020-09-29

基本的方式是使用源于 C 語(yǔ)言的反斜杠:

In [20]: multistr = " select * from multi_row \

    ...: where row_id < 5"

 

In [21]: multistr

Out[21]: ' select * from multi_row where row_id < 5'

另一個(gè)技巧是使用三引號(hào)

In [23]: multistr ="""select * from multi_row

    ...: where row_id < 5"""

 

In [24]: multistr

Out[24]: 'select * from multi_row \nwhere row_id < 5'

上面方法共有的問(wèn)題是缺少合適的縮進(jìn),如果我們嘗試縮進(jìn)會(huì)在字符串中插入空格。所以最后的解決方案是將字符串分為多行并且將整個(gè)字符串包含在括號(hào)中:

In [25]: multistr = ("select * from multi_row "

    ...: "where row_id < 5 "

    ...: "order by age")  

 

In [26]: multistr

Out[26]: 'select * from multi_row where row_id < 5 order by age'

繼續(xù)查找其他問(wèn)題的答案?

相關(guān)視頻回答
回復(fù)(0)
返回頂部