Print string to text file

Posted on

Solving problem is about exposing yourself to as many situations as possible like Print string to text file and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Print string to text file, which can be followed any time. Take easy to follow this discuss.

Print string to text file

I’m using Python to open a text document:

text_file = open("Output.txt", "w")
text_file.write("Purchase Amount: " 'TotalAmount')

I want to substitute the value of a string variable TotalAmount into the text document. Can someone please let me know how to do this?

Asked By: The Woo


Answer #1:

text_file = open("Output.txt", "w")
text_file.write("Purchase Amount: %s" % TotalAmount)

If you use a context manager, the file is closed automatically for you

with open("Output.txt", "w") as text_file:
    text_file.write("Purchase Amount: %s" % TotalAmount)

If you’re using Python2.6 or higher, it’s preferred to use str.format()

with open("Output.txt", "w") as text_file:
    text_file.write("Purchase Amount: {0}".format(TotalAmount))

For python2.7 and higher you can use {} instead of {0}

In Python3, there is an optional file parameter to the print function

with open("Output.txt", "w") as text_file:
    print("Purchase Amount: {}".format(TotalAmount), file=text_file)

Python3.6 introduced f-strings for another alternative

with open("Output.txt", "w") as text_file:
    print(f"Purchase Amount: {TotalAmount}", file=text_file)
Answered By: John La Rooy

Answer #2:

In case you want to pass multiple arguments you can use a tuple

price = 33.3
with open("Output.txt", "w") as text_file:
    text_file.write("Purchase Amount: %s price %f" % (TotalAmount, price))

More: Print multiple arguments in python

Answered By: user1767754

Answer #3:

If you are using Python3.

then you can use Print Function :

your_data = {"Purchase Amount": 'TotalAmount'}
print(your_data,  file=open('D:log.txt', 'w'))

For python2

this is the example of Python Print String To Text File

def my_func():
    this function return some value
    return 25.256
def write_file(data):
    this function write data to file
    :param data:
    file_name = r'D:log.txt'
    with open(file_name, 'w') as x_file:
        x_file.write('{} TotalAmount'.format(data))
def run():
    data = my_func()
Answered By: Rajiv Sharma

Answer #4:

If you are using numpy, printing a single (or multiply) strings to a file can be done with just one line:

numpy.savetxt('Output.txt', ["Purchase Amount: %s" % TotalAmount], fmt='%s')
Answered By: Guy s

Answer #5:

With using pathlib module, indentation isn’t needed.

import pathlib
pathlib.Path("output.txt").write_text("Purchase Amount: {}" .format(TotalAmount))

As of python 3.6, f-strings is available.

pathlib.Path("output.txt").write_text(f"Purchase Amount: {TotalAmount}")
Answered By: naoki fujita
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.