Skip to main content
 首页 » 编程设计

python之字符串python中的空格

2025年05月04日12wayfarer

Python如何检测一个字符串中是否有2个连续的空格?

例如,输入字符串:

Hello   there 

我想检测这两个空格并返回 True。

我知道您可以使用split 和join 来填充连续的空格,但是您如何检测它们呢?

请您参考如下方法:

如果你想找到两个或多个连续的空格:

if "  " in s: 
    # s contains two or more consecutive space 

如果你想在字符串中的任意位置找到两个或更多空格:

if s.count(' ') >= 2: 
    # s contains two or more spaces