python - 压缩字符串中的空格

标签 python regex

<分区>

Possible Duplicate:
Substitute multiple whitespace with single whitespace in Python

如何在 python 中将多个空格压缩为 1 个空格?

例如,假设我有一个字符串

"some   user entered     text"

我希望它变成

"some user entered text"

最佳答案

' '.join("some   user entered     text".split())

关于python - 压缩字符串中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6496884/

相关文章:

python - 如何根据第一个数组的元素从另一个数组中减去一个 NumPy 数组

python - "Solver did not exit normally"- Jupyter/Python3/Ubuntu

python - Flask-WTForms:动态创建 name 和 id 属性

c# - 正则表达式:匹配没有 > 或空格的短语

python - PyInstaller:单文件可执行文件不运行

python - 使用带有 lambda 函数的 DataFrame.apply 将返回的元组/列表分配给多个列

java - 从富文本中提取base64字符串并将其收集在数组中

Linux 文件权限的正则表达式(数字符号)

Python 正则表达式在>之后使用正向lookbehind进行匹配

string - 捕获量化正则表达式中的所有匹配项