146xwq

一.去除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))

 

分类:

技术点:

相关文章:

  • 2021-12-25
  • 2021-11-15
  • 2021-11-17
  • 2021-11-08
  • 2021-10-15
  • 2021-11-17
  • 2021-08-02
  • 2021-09-23
猜你喜欢
  • 2021-11-17
  • 2021-07-24
  • 2021-11-17
  • 2021-12-15
  • 2021-11-17
  • 2021-11-27
  • 2021-11-17
相关资源
相似解决方案