Writing to a file without formatting using write()

If we don't care about how the output looks, but just want to get the numbers out, here is another way to do it with the write() method:

fp.write(str(x) + " " + str(x*x) + "\n"))

In this case we built the string using concatenation (+) and with the Python string conversion function str. Notice that, for readability, we have inserted an explicit space between the numbers and ended the line with a new-line character.