I refined the code by using rstrip(); also output with "\n".join.