您可以结合使用前瞻断言re.sub():import re s = ' I love cats're.sub(r'''^ # match beginning of string \s+ # match one or more instances of whitespace (?=[A-Z]) # positive lookahead assertion of an uppercase character ''','',s,flags=re.VERBOSE)并向您展示在小写字母之前没有删除空格:s = ' this is a test're.sub(r'^\s+(?=[A-Z])','',s)结果:' this is a test'