Write Multiple Variables to a File using Python
Last Updated :
12 Apr, 2025
Storing multiple variables in a file is a common task in programming, especially when dealing with data persistence or configuration settings. In this article, we will explore three different approaches to efficiently writing multiple variables in a file using Python. Below are the possible approaches to writing multiple variables to a file in Python.
- Using the repr() function
- Using the string formatting
- Using the str() function
Using the repr() function
In this approach we are using the repr() function to convert each variable into its string representation, preserving its data type andwrite them to a file named 'output.txt' in the format of variable assignments, allowing for easy reconstruction of the variables when read back from the file.
Python
n = "GeeksforGeeks"
f = 2009
p = True
with open('output.txt', 'w') as file:
file.write("website_name = " + repr(n) + '\n')
file.write("founded_year = " + repr(f) + '\n')
file.write("is_popular = " + repr(p) + '\n')
Output
website_name = 'GeeksforGeeks'
founded_year = 2009
is_popular = True
Explanation: This code writes the values of three variables n, f, p (which are website name, founded year and bool for is popular) to a file (output.txt) with their names and repr() representations.
Using the string formatting
In this approach, we are using string formatting to write multiple variables (n, f and p) to a file named 'output.txt', creating a clear and human-readable representation of the data with formatted labels and values.
Python
n = "GeeksforGeeks"
f = 2009
p = True
with open('output.txt', 'w') as file:
file.write("Website Name: {}\n".format(n))
file.write("Founded Year: {}\n".format(f))
file.write("Is Popular: {}\n".format(p))
Output
website_name = 'GeeksforGeeks'
founded_year = 2009
is_popular = True
Using the str() function
In this approach, we are using the str() function to convert each variable (n, f and p) into strings and writes them to a file named 'output.txt', presenting a proper representation of the variables with labeled values.
Python
n = "GeeksforGeeks"
f = 2009
p = True
with open('output.txt', 'w') as file:
file.write("Website Name: " + str(n) + '\n')
file.write("Founded Year: " + str(f) + '\n')
file.write("Is Popular: " + str(p) + '\n')
Output
website_name = 'GeeksforGeeks'
founded_year = 2009
is_popular = True
Related Articles:
Similar Reads
Create a File Path with Variables in Python The task is to create a file path using variables in Python. Different methods we can use are string concatenation and os.path.join(), both of which allow us to build file paths dynamically and ensure compatibility across different platforms. For example, if you have a folder named Documents and a f
3 min read
How to Use Words in a Text File as Variables in Python We are given a txt file and our task is to find out the way by which we can use word in the txt file as a variable in Python. In this article, we will see how we can use word inside a text file as a variable in Python. Example: Input: fruits.txt apple banana orange Output: apple = Fruit banana = Fru
3 min read
Write Os.System Output In File Using Python Python is a high-level programming language. There are many modules. However, we will use os.system module in this Program. This module provides a portable way of using operating system-dependent functionality. The "os" and "os.path()" modules include many functions to interact with the file system.
3 min read
Insert a Variable into a String - Python The goal here is to insert a variable into a string in Python. For example, if we have a variable containing the word "Hello" and another containing "World", we want to combine them into a single string like "Hello World". Let's explore the different methods to insert variables into strings effectiv
2 min read
Replace Multiple Lines From A File Using Python In Python, replacing multiple lines in a file consists of updating specific contents within a text file. This can be done using various modules and their associated functions. In this article, we will explore three different approaches along with the practical implementation of each approach in term
3 min read
Ways To Save Python Terminal Output To A Text File In Python, saving terminal output to a text file is a common need when you want to keep a record of what your program prints. Whether you're debugging code, logging results or simply organizing your workflow, capturing the output allows you to review it later without having to rerun the program. Let
3 min read