1.去除字符串开头和结尾的空格,strip()方法
>>> a = " a b c "
>>> a.strip()
'a b c'
2.去除字符串开头的空格,lstrip()方法
>>> a = " a b c "
>>> a.lstrip()
'a b c '
3.去除字符串结尾的空格,rstrip()方法
>>> a = " a b c "
>>> a.rstrip()
' a b c'
4.去除全部空格
1.去除全部空格
replace主要用于字符串的替换replace(old, new, count)
>>> a = " a b c "
>>> a.replace(" ", "")
'abc'
2.replace主要用于字符串的替换replace(old, new, count)
join可以将列表每个字符通过前字符串进行拼接,split可以将字符串按指定规则进行切割生成一个新的列表,不传参默认按空格切割
>>> a = " a b c "
>>> b = a.split()
>>> c = "".join(b)
>>> c
'abc'
>>> a = " a b c "
>>> "".join(a.split())
'abc'
|