Prerequisites: File Handling in Python
Given a text file and the task is to read the information from file word by word in Python.
I am R2J!
Geeks 4 Geeks
And in that dream, we were flying.
- Open a file in read mode which contains a string.
forloop to read each line from the text file.
- Again use
forloop to read each word from the line splitted by ‘ ‘.
- Display each word from each line in the text file.
Example 1: Let’s suppose the text file looks like this –
Geeks 4 geeks
Example 2: Let’s suppose the text file contains more than one line.
Geeks 4 Geeks And in that dream, we were flying.
- Python | Count occurrences of each word in given text file (Using dictionary)
- Python program to remove Nth occurrence of the given word
- Python | Program that matches a word containing 'g' followed by one or more e's using regex
- Python program for word guessing game
- Python program to capitalize the first and last character of each word in a string
- Python | Program to implement Jumbled word game
- Program to replace a word with asterisks in a sentence
- Python program to read character by character from a file
- Python | Word Stretch
- Python - Get Nth word in given String
- Python - Separate first word from String
- Generating Word Cloud in Python
- Python | Reverse each word in a sentence
- Python - Word starting at Index
- Python - Kth word replace in String
- Python | Word Similarity using spaCy
- Second most repeated word in a sequence in Python
- Python | Word location in String
- Python | Word Embedding using Word2Vec
- Generating Word Cloud in Python | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.