def strip_eol(text: str) -> str: """Remove each whitespace from end of lines of string and also remove trailing empty lines.""" result = '\n'.join(line.rstrip() for line in text.splitlines(False)) result = result.rstrip() if len(result) > 0: return result + '\n' return ''