Write Multiple Variables to a File using Python Last Updated : 12 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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() functionUsing the string formattingUsing the str() functionUsing the repr() functionIn 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') Outputwebsite_name = 'GeeksforGeeks'founded_year = 2009is_popular = TrueExplanation: 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 formattingIn 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)) Outputwebsite_name = 'GeeksforGeeks'founded_year = 2009is_popular = TrueUsing the str() functionIn 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') Outputwebsite_name = 'GeeksforGeeks'founded_year = 2009is_popular = TrueRelated Articles:Python repr() FunctionPython String FormattingPython str() functionPython Tutorial Comment More infoAdvertise with us Next Article Write Multiple Variables to a File using Python A anjalibo6rb0 Follow Improve Article Tags : Python Python Programs Python file-handling-programs Practice Tags : python 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 How to Write a Configuration File in Python? Configuring your Python applications using configuration files is a common practice that allows you to separate configuration details from your code, making it more modular and easier to manage. In this article, we'll explore how to create a configuration file in Python, focusing on a simple and wid 3 min read How To Create A Csv File Using Python CSV stands for comma-separated values, it is a type of text file where information is separated by commas (or any other delimiter), they are commonly used in databases and spreadsheets to store information in an organized manner. In this article, we will see how we can create a CSV file using Python 3 min read File System Manipulation in Python File system manipulation in Python refers to the ability to perform various operations on files, such as creating, reading, writing, appending, renaming, and deleting. Python provides several built-in modules and functions that allow you to perform various file system operations. Python treats files 3 min read How To Write A Multidimensional Array To A Text File? In this article, we will explain how to write a multidimensional array to a text file with the help of a few examples. Writing a multidimensional array to a text file is a straightforward process, and it allows you to print the array directly to the text file in a simple manner. Write A Multidimensi 3 min read Like