1、使用strip()、lstrip()、rstrip()等方法删除字符串中的空格(这些方法只能删除字符串两端的空格)
" <a href="https://www.gaodaima.com/tag/xyz" title="查看更多关于xyz的文章" target="_blank">xyz</a> ".strip() # returns "xyz" " xyz ".lstrip() # returns "xyz " " xyz ".rstrip() # returns " xyz"
www#gaodaima.com来源[email protected]搞@^&代*@码网搞代码
2、使用replace()方法将空格替换为空
" x y z ".replace(' ', '') # returns "xyz"
3、使用replace("
", "")方法将换行符替换为空。
更多Python知识请关注搞代码网。
来源:搞代码网:原文地址:https://www.gaodaima.com