python字符串去除空格,python去除字符串(string)空格的五种方法
成年人的愛情不僅僅是簡單的我愛你和漂亮的新衣服。
python去掉字符串中的空格的方法總結
1、strip方法去掉字符串兩邊(開頭和結尾)的空格
space_str = ' Remove the space around the string '
print(space_str.strip())
2、lstrip方法去掉字符串左邊的空格
left_space_str = ' Remove the space to the left of the string '
print(left_space_str.lstrip())
3、rstrip方法去掉字符串右邊的空格
right_space_str = ' Remove the space to the right of the string '
print(right_space_str.rstrip())
4、replace方法替換字符串的空格為空
all_space_str = ' The space of the replacement string is empty '
print(all_space_str.replace(' ', '')
注意:這里說一下replace方法的具體用法
str.replace(old_str, new_str, [max])
old_str:代表原來的字符串,new_str:代表替換之后的字符串,max:代表替換的次數
5、正則匹配替換空格
import re
all_str = " Regular matching without spaces "
print(re.sub(r"\s+", "", all_str))
正則方法的使用這里不多說了,自己查一下詳細文檔即可。
如果感覺本文對您有幫助可以點個贊哦
本文為學習筆記,轉載請標明出處
本文僅供交流學習,請勿用于非法途徑
僅是個人意見,如有想法,歡迎留言
總結
以上是生活随笔為你收集整理的python字符串去除空格,python去除字符串(string)空格的五种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用数据库实现缓存功能
- 下一篇: Android 网页h5 Input选择