Skip to main content

10.8) Joining Strings


Joining strings with join(), introduced in level 2, is also useful in processing text before printing it to a file. For example:

L=[12.3,45.6,78.9]
strL=[str(i) for i in L]  # convert numbers in list to strings
# create csv output
csv_output=(',').join(strL)
f=open('output.csv','w')
f.write(csv_output)
f.close()
# create column output
column_output=('\n').join(strL)
f=open('output.dat','w')
f.write(column_output)
f.close()

In following sections, we’ll learn an easier way to write lists and arrays of numbers into csv or tab files with Numpy.