我想从字符串,两端和单词之间消除所有空格 .
我有这个Python代码:
def my_handle(self): sentence = ' hello apple ' sentence.strip()
但这只会消除字符串两边的空白 . 如何删除所有空格?
空白包括 space, tabs, and CRLF . 所以我们可以使用的优雅和 one-liner 字符串函数是 translate :
' hello apple'.translate(None, ' \n\t\r')
OR 如果你想彻底:
import string ' hello apple'.translate(None, string.whitespace)
要从开头和结尾删除空格,请使用 strip .
strip
>> " foo bar ".strip() "foo bar"
' hello \n\tapple'.translate( { ord(c):None for c in ' \n\t\r' } )
MaK已经指出了上面的"translate"方法 . 这种变体适用于Python 3(参见this Q&A) .
import re sentence = ' hello apple' re.sub(' ','',sentence) #helloworld (remove all spaces) re.sub(' ',' ',sentence) #hello world (remove double spaces)
小心:
strip 执行rstrip和lstrip(删除前导和尾随空格,制表符,返回和换页符,但它不会在字符串中间删除它们) .
如果只替换空格和制表符,最终可能会出现与您要查找的内容相匹配的隐藏CRLF,但不一样 .
如果要删除前导和结尾空格,请使用str.strip():
sentence = ' hello apple' sentence.strip() >>> 'hello apple'
如果要删除所有空格,请使用str.replace():
sentence = ' hello apple' sentence.replace(" ", "") >>> 'helloapple'
如果要删除重复的空格,请使用str.split():
sentence = ' hello apple' " ".join(sentence.split()) >>> 'hello apple'
要仅删除空格,请使用str.replace:
sentence = sentence.replace(' ', '')
要删除所有空白字符(空格,制表符,换行符等),您可以使用split然后join:
sentence = ''.join(sentence.split())
或正则表达式:
import re pattern = re.compile(r'\s+') sentence = re.sub(pattern, '', sentence)
如果您只想从开头和结尾删除空格,可以使用strip:
sentence = sentence.strip()
您还可以使用lstrip仅从字符串的开头删除空格,并使用rstrip从字符串末尾删除空格 .
另一种方法是使用正则表达式并匹配these strange white-space characters . 这里有些例子:
Remove ALL spaces in a string, even between words:
import re sentence = re.sub(r"\s+", "", sentence, flags=re.UNICODE)
Remove spaces in the BEGINNING of a string:
import re sentence = re.sub(r"^\s+", "", sentence, flags=re.UNICODE)
Remove spaces in the END of a string:
import re sentence = re.sub(r"\s+$", "", sentence, flags=re.UNICODE)
Remove spaces both in the BEGINNING and in the END of a string:
import re sentence = re.sub("^\s+|\s+$", "", sentence, flags=re.UNICODE)
Remove ONLY DUPLICATE spaces:
import re sentence = " ".join(re.split("\s+", sentence, flags=re.UNICODE))
(所有示例都适用于Python 2和Python 3)
此外,strip有一些变化:
Remove spaces in the BEGINNING and END of a string:
sentence= sentence.strip()
sentence = sentence.lstrip()
sentence= sentence.rstrip()
所有三个字符串函数 strip lstrip 和 rstrip 都可以将字符串的参数删除,默认为全空格 . 当您使用某些特定内容时,这可能会有所帮助,例如,您只能删除空格而不能删除换行符:
lstrip
rstrip
" 1. Step 1\n".strip(" ")
或者,您可以在读取字符串列表时删除额外的逗号:
"1,2,3,".strip(",")
9 回答
空白包括 space, tabs, and CRLF . 所以我们可以使用的优雅和 one-liner 字符串函数是 translate :
OR 如果你想彻底:
要从开头和结尾删除空格,请使用
strip
.MaK已经指出了上面的"translate"方法 . 这种变体适用于Python 3(参见this Q&A) .
小心:
strip
执行rstrip和lstrip(删除前导和尾随空格,制表符,返回和换页符,但它不会在字符串中间删除它们) .如果只替换空格和制表符,最终可能会出现与您要查找的内容相匹配的隐藏CRLF,但不一样 .
如果要删除前导和结尾空格,请使用str.strip():
如果要删除所有空格,请使用str.replace():
如果要删除重复的空格,请使用str.split():
要仅删除空格,请使用str.replace:
要删除所有空白字符(空格,制表符,换行符等),您可以使用split然后join:
或正则表达式:
如果您只想从开头和结尾删除空格,可以使用strip:
您还可以使用lstrip仅从字符串的开头删除空格,并使用rstrip从字符串末尾删除空格 .
另一种方法是使用正则表达式并匹配these strange white-space characters . 这里有些例子:
Remove ALL spaces in a string, even between words:
Remove spaces in the BEGINNING of a string:
Remove spaces in the END of a string:
Remove spaces both in the BEGINNING and in the END of a string:
Remove ONLY DUPLICATE spaces:
(所有示例都适用于Python 2和Python 3)
此外,strip有一些变化:
Remove spaces in the BEGINNING and END of a string:
Remove spaces in the BEGINNING of a string:
Remove spaces in the END of a string:
所有三个字符串函数
strip
lstrip
和rstrip
都可以将字符串的参数删除,默认为全空格 . 当您使用某些特定内容时,这可能会有所帮助,例如,您只能删除空格而不能删除换行符:或者,您可以在读取字符串列表时删除额外的逗号: