首页 > 解决方案 > 如何从字符串中修剪空格(左右和空格之间)?

问题描述

我想删除句子中的任何额外空格我怎么能用python做到这一点?例如:

("  hello   world") >>>  ("hello world")

标签: python

解决方案


str = " hello     world "
str_clean = ' '.join(str.split())
print(str_clean)

hello world

编辑,我想我忽略了你的初始文本。


推荐阅读