python 去掉空格_trim - 删除Python中字符串中的所有空格

trim - 删除Python中字符串中的所有空格

我想从字符串,两端和单词之间消除所有空格。

我有这个Python代码:

def my_handle(self):

sentence = ' hello apple '

sentence.strip()

但这只会消除字符串两边的空白。 如何删除所有空格?

9个解决方案

1251 votes

如果要删除前导和结束空格,请使用str.split():

sentence = ' hello apple'

sentence.strip()

>>> 'hello apple'

如果要删除所有空格,请使用str.split():

sentence = ' hello apple'

sentence.replace(" ", "")

>>> 'helloapple'

如果要删除重复的空格,请使用str.split():

sentence = ' hello apple'

" ".join(sentence.split())

>>> 'hello apple'

Cédric Julien answered 2019-01-18T07:08:53Z

208 votes

要仅删除空格,请使用str.replace:

sentence = sentence.replace(' ', '')

要删除所有空格字符(空格,制表符,换行符等),您可以使用rstrip,然后使用rstrip:

sentence = ''.join(sentence.split())

或正则表达式:

import re

pattern = re.compile(r'\s+')

sentence = re.sub(pattern, '', sentence)

如果您只想从开头和结尾删除空格,可以使用rstrip:

sentence = sentence.strip()

您也可以使用rstrip仅从字符串的开头删除空格,并使用rstrip从字符串末尾删除空格。

Mark Byers answered 2019-01-18T07:09:43Z

64 votes

另一种方法是使用正则表达式并匹配这些奇怪的空白字符。 这里有些例子:

删除字符串中的所有空格,即使在单词之间:

import re

sentence = re.sub(r"\s+", "", sentence, flags=re.UNICODE)

删除字符串BEGINNING中的空格:

import re

sentence = re.sub(r"^\s+", "", sentence, flags=re.UNICODE)

删除字符串END中的空格:

import re

sentence = re.sub(r"\s+$", "", sentence, flags=re.UNICODE)

删除BEGINNING和字符串END中的空格:

import re

sentence = re.sub("^\s+|\s+$", "", sentence, flags=re.UNICODE)

仅删除DUPLICATE空格:

import re

sentence = " ".join(re.split("\s+", sentence, flags=re.UNICODE))

(所有示例都适用于Python 2和Python 3)

Emil Stenström answered 2019-01-18T07:10:46Z

30 votes

空格包括空格,制表符和CRLF。 所以我们可以使用的优雅和单行字符串函数是:

' hello apple'.translate(None, ' \n\t\r')

或者如果你想彻底:

import string

' hello apple'.translate(None, string.whitespace)

MaK answered 2019-01-18T07:11:15Z

18 votes

要从开头和结尾删除空格,请使用strip。

>> " foo bar ".strip()

"foo bar"

wal-o-mat answered 2019-01-18T07:11:37Z

6 votes

' hello \n\tapple'.translate( { ord(c):None for c in ' \n\t\r' } )

MaK已经指出了上面的“翻译”方法。 这种变化适用于Python 3(参见本Q& A)。

Amnon Harel answered 2019-01-18T07:11:59Z

3 votes

import re

sentence = ' hello apple'

re.sub(' ','',sentence) #helloworld (remove all spaces)

re.sub(' ',' ',sentence) #hello world (remove double spaces)

PrabhuPrakash answered 2019-01-18T07:12:15Z

3 votes

小心:

strip执行rstrip和lstrip(删除前导和尾随空格,制表符,返回和换页符,但它不会在字符串的中间删除它们)。

如果只替换空格和制表符,最终可能会出现与您要查找的内容相匹配的隐藏CRLF,但不一样。

yan bellavance answered 2019-01-18T07:12:51Z

2 votes

另外,strip有一些变化:

删除字符串BEGINNING和END中的空格:

sentence= sentence.strip()

删除字符串BEGINNING中的空格:

sentence = sentence.lstrip()

删除字符串END中的空格:

sentence= sentence.rstrip()

所有三个字符串函数strip lstrip和rstrip可以将字符串的参数带到条带,默认为全白空间。 当您使用某些特定内容时,这可能会有所帮助,例如,您只能删除空格而不能删除换行符:

" 1. Step 1\n".strip(" ")

或者,您可以在读取字符串列表时删除额外的逗号:

"1,2,3,".strip(",")

Anna answered 2019-01-18T07:13:37Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值