Python3 str去除空格
2022/3/2 17:16:38
本文主要是介绍Python3 str去除空格,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
转自:https://www.cnblogs.com/146xwq/p/9684891.html
一.去除str两端空格(strip())
a.去除左端空格 lstrip()
str0 = 'abcdef'
str1 = ' abcdef'
print (str0)
print (str1.lstrip())
|
b.去除右端空格 rstrip()
str0='abcdef ' str1='abcdef ' print(str0) print(str1.rstrip())
c.去除两端空格 strip()
str0=' abcdef ' str1=' abcdef ' print(str0) print(str1.strip())
二.str空格替换(replace() 推荐使用)
str0=' a b c d e f ' str1=' a b c d e f ' print(str0) print(str1.replace(' ',''))
三.str重新组合替换空格(使用split分割字符,后重新组合,效率低)
str0=' a b c d e f ' str1=' a b c d e f ' print(str0) print(''.join(str1.split()))
四.正则表达式去空格(,用sub()方法替换)
import re str0=' a b c d e f ' str1=' a b c d e f ' print(str0)print(re.sub(' ','',str1))
这篇关于Python3 str去除空格的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python