If you play with it, you can make this more compact.
I'd do it myself, but getting tired.
I'd do it myself, but getting tired.
import re s = 'abc "this that" xyz' i1, i2 = [match.start() for match in re.finditer('"', s)] s1 = s[:i1] for char in s[i1+1:]: if char == '"': break if char != ' ': s1 += '_' else: s1 += ' ' s1 += s[i2+1:] print(s1)prints:
Output:abc ____ ____ xyz