Python Programming - Reading and Writing Files - Discussion

Discussion Forum : Reading and Writing Files - General Questions (Q.No. 100)
100.
How can you read and print the first word from each line in a file named "text.txt"?
with open("text.txt", "r") as file:
    for line in file:
        print(line.split()[0])
print_first_words("text.txt")
with open("text.txt", "r") as file:
    print(file.readline().split()[0])
with open("text.txt", "r") as file:
    print(file.read(1).split()[0])
Answer: Option
Explanation:
Using line.split()[0] to get the first word from each line.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.