How to Use Words in a Text File as Variables in Python
Last Updated :
03 May, 2024
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 = Fruit
orange = Fruit
Explanation: Here, we are using the content inside the fruits text file indicating that all the values inside the file is a fruit.
How to Use Words in a Text File as Variables in Python
Below are some of the ways by which we can use words in a text file as a variable in Python:
fruits.txt
apple
banana
orange
Reading Words from a Text File
In this example, a Python script reads words from a file named "fruits.txt" and assigns "Fruit" to each word as a label. It achieves this by first storing the file's lines and then iterating through them, stripping whitespace and adding each word to a list. Finally, it loops through the word list, printing each word followed by " = Fruit".
Python
# Open the file
with open("fruits.txt", "r") as file:
# Read all lines from the file
lines = file.readlines()
# Initialize an empty list to store the words
words = []
# Loop through each line in the file
for line in lines:
word = line.strip()
words.append(word)
# Now you can use the words as variables
for word in words:
print(f"{word} = Fruit")
Output:
apple = Fruit
banana = Fruit
orange = Fruit
Using List Comprehension
In this example, a Python script efficiently reads words from "words.txt". It employs a single line of list comprehension to achieve this. The list comprehension iterates through each line in the file, removes whitespace with strip()
, and builds a list containing all the words.
Python
# Read words from the file using list comprehension
with open("words.txt", "r") as file:
words = [line.strip() for line in file]
# Now you can use the words as variables
for word in words:
print(f"{word} = Fruit")
Output:
apple = Fruit
banana = Fruit
orange = Fruit
Using File Iteration
In this example, a concise Python script processes a text file "words.txt". It iterates through each line, removes whitespace using strip()
, and directly prints each word labeled as "Fruit".
Python
# Open the file
with open("words.txt", "r") as file:
for line in file:
word = line.strip()
print(f"{word} = Fruit")
Output:
apple = Fruit
banana = Fruit
orange = Fruit
Similar Reads
Write Multiple Variables to a File using Python 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 approach
2 min read
Count Words in Text File in Python Our task is to create a Python program that reads a text file, counts the number of words in the file and prints the word count. This can be done by opening the file, reading its contents, splitting the text into words, and then counting the total number of words.Example 1: Count String WordsFirst,
3 min read
How to take string as input from a user in Python Accepting input is straightforward and very user-friendly in Python because of the built-in input() function. In this article, weâll walk through how to take string input from a user in Python with simple examples. The input() function allows us to prompt the user for input and read it as a string.
3 min read
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
Python | Finding 'n' Character Words in a Text File This article aims to find words with a certain number of characters. In the code mentioned below, a Python program is given to find the words containing three characters in the text file. Example Input: Hello, how are you ? , n=3 Output: how are you Explanation: Output contains every character of th
3 min read
Append Text or Lines to a File in Python Appending text or lines to a file is a common operation in programming, especially when you want to add new information to an existing file without overwriting its content. In Python, this task is made simple with built-in functions that allow you to open a file and append data to it. In this tutori
3 min read