![]() |
| Formatting to output file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Formatting to output file (/thread-36059.html) |
Formatting to output file - Mark17 - Jan-13-2022 If I wanted to format decimal places in a print line, then I could do: s = 37.3829 print('This is an example of formatting to 2 decimal places: {:.2f}'.format(s))What's the syntax for doing such formatting if printing to output_file.csv ? RE: Formatting to output file - ndc85430 - Jan-13-2022 You're misunderstanding - there's no difference, because you're creating a string with the format you want and then passing that to print or the function that will write the line in the file. RE: Formatting to output file - BashBedlam - Jan-13-2022 ...and here's an example to illustrate: s = 37.3829 output_string = 'This is an example of formatting to 2 decimal places: {:.2f}'.format(s) print (output_string) with open ('tester.txt', 'w') as tester_file : tester_file.write (output_string) |