def subtract_common_whitespace(s):
	left = 0
	while left < len(s) and s[left].isspace():
		left += 1

	right = len(s) - 1
	while right >= 0 and s[right].isspace():
		right -= 1

	ws_left = 0
	ws_right = right + 1
	while ws_right < len(s) and s[ws_left] == s[ws_right]:
		ws_left += 1
		ws_right += 1

	return s[ws_left:left] + s[left:right+1] + s[ws_right:]

print(repr(subtract_common_whitespace("\n  abc\n "))) # -> "abc"
print(repr(subtract_common_whitespace("\n  abc "))) # -> "\n abc  "
print(repr(subtract_common_whitespace("\n abc\n  "))) # -> "abc "

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: