Use sys.stdout.write instead of print to avoid newline.