删除字符串中的所有空格
我想消除字符串两端和单词之间的所有空格。
我有这个Python代码:
def my_handle(self):
sentence = ' hello apple '
sentence.strip()
但这只能消除字符串两侧的空格。如何删除所有空格?
I want to eliminate all the whitespace from a string, on both ends, and in between words.
I have this Python code:
def my_handle(self):
sentence = ' hello apple '
sentence.strip()
But that only eliminates the whitespace on both sides of the string. How do I remove all whitespace?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
如果要删除开头和结尾的空格,请使用
str.strip ()
:如果要删除所有空格字符,请使用
str.replace()
(注意,这只会删除“正常”ASCII 空格字符' ' U+0020
但不是任何其他空格):如果您想删除所有空格,然后之间留一个空格字符的话,使用
str.split()
后跟str.join()
:如果要删除所有 空格然后更改上面的内容将
" "
引导至""
:If you want to remove leading and ending whitespace, use
str.strip()
:If you want to remove all space characters, use
str.replace()
(NB this only removes the “normal” ASCII space character' ' U+0020
but not any other whitespace):If you want to remove all whitespace and then leave a single space character between words, use
str.split()
followed bystr.join()
:If you want to remove all whitespace then change the above leading
" "
to""
:要仅删除空格,请使用
str.replace
:要删除所有空白字符(空格、制表符、换行符等),您可以使用
分割
,然后join
:或正则表达式:
如果您只想删除开头和结尾的空格,可以使用<一href="http://docs.python.org/2/library/stdtypes.html#str.strip">
strip
:您还可以使用
lstrip
仅删除字符串开头的空格,以及rstrip
从字符串末尾删除空格。To remove only spaces use
str.replace
:To remove all whitespace characters (space, tab, newline, and so on) you can use
split
thenjoin
:or a regular expression:
If you want to only remove whitespace from the beginning and end you can use
strip
:You can also use
lstrip
to remove whitespace only from the beginning of the string, andrstrip
to remove whitespace from the end of the string.另一种方法是使用正则表达式并匹配这些奇怪的空白字符。以下是一些示例:
删除字符串中的所有空格,甚至单词之间的空格:
删除字符串开头的空格
删除字符串结尾的空格:
删除字符串开头和结尾处的空格:
仅删除重复的空格:
(所有示例都适用于 Python 2 和 Python 3)
An alternative is to use regular expressions and match these strange white-space characters too. Here are some examples:
Remove ALL whitespace in a string, even between words:
Remove whitespace in the BEGINNING of a string:
Remove whitespace in the END of a string:
Remove whitespace both at the BEGINNING and at the END of a string:
Remove ONLY DUPLICATE whitespace:
(All examples work in both Python 2 and Python 3)
“空白”包括空格、制表符和CRLF。因此,我们可以使用的一个优雅的单行字符串函数是
str.translate
:Python 3
OR(如果你想彻底):
Python 2
OR(如果你想彻底)彻底:
"Whitespace" includes space, tabs, and CRLF. So an elegant and one-liner string function we can use is
str.translate
:Python 3
OR if you want to be thorough:
Python 2
OR if you want to be thorough:
要删除开头和结尾的空格,请使用
strip
。For removing whitespace from beginning and end, use
strip
.MaK在上面已经指出了“翻译”的方法。此变体适用于 Python 3(请参阅 此问答)。
MaK already pointed out the "translate" method above. And this variation works with Python 3 (see this Q&A).
此外,strip 有一些变化:
删除开头和中的空格字符串结尾:
删除字符串开头的空格:
删除字符串末尾的空格:
所有三个字符串函数
strip< /代码>
lstrip
和rstrip
可以获取要剥离的字符串参数,默认为全空格。当您处理特定内容时,这会很有帮助,例如,您可以仅删除空格而不是换行符:或者您可以在读取字符串列表时删除多余的逗号:
In addition, strip has some variations:
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:
All three string functions
strip
lstrip
, andrstrip
can take parameters of the string to strip, with the default being all white space. This can be helpful when you are working with something particular, for example, you could remove only spaces but not newlines:Or you could remove extra commas when reading in a string list:
请注意:
strip
执行 rstrip 和 lstrip(删除前导和尾随空格、制表符、回车符和换页符,但不会删除字符串中间的它们)。如果您只替换空格和制表符,您最终可能会得到隐藏的 CRLF,这些 CRLF 看起来与您要查找的内容匹配,但并不相同。
Be careful:
strip
does a rstrip and lstrip (removes leading and trailing spaces, tabs, returns and form feeds, but it does not remove them in the middle of the string).If you only replace spaces and tabs you can end up with hidden CRLFs that appear to match what you are looking for, but are not the same.
Python 文档:
Python docs:
我使用 split() 忽略所有空格并使用 join() 来连接
字符串。
我更喜欢这种方法,因为它只是一个表达式(而不是语句)。
它易于使用,无需绑定变量即可使用。
I use split() to ignore all whitespaces and use join() to concatenate
strings.
I prefer this approach because it is only a expression (not a statement).
It is easy to use and it can use without binding to a variable.
在下面的脚本中,我们导入正则表达式模块,用于用一个空格替换一个或多个空格。这确保了内部多余的空间被删除。然后我们使用 strip() 函数删除前导和尾随空格。
In the following script we import the regular expression module which we use to substitute one space or more with a single space. This ensures that the inner extra spaces are removed. Then we use strip() function to remove leading and trailing spaces.
Python 3 中所有字符串字符都是 unicode 文字;因此,由于
str.split()
在所有空白字符上进行拆分,这意味着它会在 unicode 空白字符上进行拆分。所以split
+join
语法(如1,
2,
3) 将产生与带有 UNICODE 标志的
re.sub
相同的输出(如 < a href="https://stackoverflow.com/a/28607213/19123103">4);事实上,UNICODE 标志在这里是多余的(如2,
5,
6,
7)。
在性能方面,由于Python的字符串方法经过了优化,因此它们比正则表达式快得多。正如下面的 timeit 测试所示,当从 OP 中的字符串中删除所有空白字符时,Python 字符串方法比
re
选项快 7 倍以上。All string characters are unicode literal in Python 3; as a consequence, since
str.split()
splits on all white space characters, that means it splits on unicode white space characters. Sosplit
+join
syntax (as in1,
2,
3) will produce the same output as
re.sub
with the UNICODE flag (as in 4); in fact, the UNICODE flag is redundant here (as in2,
5,
6,
7).
In terms of performance, since Python's string methods are optimized, they are much faster than regex. As the following timeit test shows, when removing all white space characters from the string in the OP, Python string methods are over 7 times faster than
re
option.我发现这对我来说最有效:
它删除了所有空格、制表符等。
I found that this works the best for me:
It removes any whitespaces, tabs, etc.
只是除了 Emil Stenström 的答案
这段代码会删除所有空格,您也可以删除自己的额外 utf-8 字符。
Just addition to the Emil Stenström's answer
This code removes all white spaces and you could also remove your own extra utf-8 characters.
试试这个..而不是使用 re 我认为使用 split 和 strip 更好
try this.. instead of using re i think using split with strip is much better