Python3 str去除空格
2021-06-21 05:06
                         标签:split   替换空格   正则表达式   nbsp   pytho   port   color   style   ace    一.去除str两端空格(strip())   a.去除左端空格  lstrip()   b.去除右端空格 rstrip()   c.去除两端空格  strip() 二.str空格替换(replace()     推荐使用) 三.str重新组合替换空格(使用split分割字符,后重新组合,效率低) 四.正则表达式去空格(,用sub()方法替换)   Python3 str去除空格 标签:split   替换空格   正则表达式   nbsp   pytho   port   color   style   ace    原文地址:https://www.cnblogs.com/146xwq/p/9684891.htmlstr0=‘abcdef‘
str1=‘  abcdef‘
print(str0)
print(str1.lstrip())
str0=‘abcdef  ‘
str1=‘abcdef  ‘
print(str0)
print(str1.rstrip())
str0=‘  abcdef  ‘
str1=‘  abcdef  ‘
print(str0)
print(str1.strip())
str0=‘  a b c d e f  ‘
str1=‘  a b c d e f  ‘
print(str0)
print(str1.replace(‘ ‘,‘‘))
str0=‘  a b c d e f  ‘
str1=‘  a b c d e f  ‘
print(str0)
print(‘‘.join(str1.split()))
import re
str0=‘  a b c d e f  ‘
str1=‘  a b c d e f  ‘
print(str0)print(re.sub(‘ ‘,‘‘,str1))
上一篇:JavaScript学习摘要
下一篇:MFC 字体