在Python 修剪空白
要從Python 中的字串中刪除空白字元(空格和空格個選項可用:
兩邊的空白:
<code class="python">s = " \t example string\t " s = s.strip()</code>
對於右側的空格:
<code class="python">s = s.rstrip()</code>
對於左邊的空格:
<code class="python">s = s.lstrip()</code>
剝離特定字元:
s = s.strip(' \t\n\r')<p></p>這將從字串兩側刪除任何空格、製表符、換行符或回車符。 <p><strong></strong> 刪除字串中間的空格:</p>
<p></p>要刪除字串中間的空格,可以使用正規表示式:<pre class="brush:php;toolbar:false"><code class="python">import re
print(re.sub('[\s+]', '', s))</code></pre>
<p></p>這將從字串中刪除所有連續的空白字元序列。
以上是以下是一些根據問答格式自訂的標題選項: **專注於具體技術:** * **如何在 Python 中修剪空格:strip()、rstrip()、lstrip()** * **去除白粉病的詳細內容。更多資訊請關注PHP中文網其他相關文章!