python應(yīng)用操作——多行字符串
基本的方式是使用源于 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'
點(diǎn)擊加載更多評(píng)論>>