Pulling a random word or string from a line in a text file in Python - GeeksforGeeks (2024)

Improve

Improve

Like Article

Like

Save

Report

File handling in Python is really simple and easy to implement. In order to pull a random word or string from a text file, we will first open the file in read mode and then use the methods in Python’s random module to pick a random word.

There are various ways to perform this operation:

This is the text file we will read from:

Pulling a random word or string from a line in a text file in Python - GeeksforGeeks (1)

Method 1: Using random.choice()

Steps:

  1. Using with function, open the file in read mode. The with function takes care of closing the file automatically.
  2. Read all the text from the file and store in a string
  3. Split the string into words separated by space.
  4. Use random.choice() to pick a word or string.

Python

# Python code to pick a random

# word from a text file

import random

# Open the file in read mode

with open("MyFile.txt", "r") as file:

allText = file.read()

words = list(map(str, allText.split()))

# print random string

print(random.choice(words))

Note: The split() function, by default, splits by white space. If you want any other delimiter like newline character you can specify that as an argument.

Output:

Pulling a random word or string from a line in a text file in Python - GeeksforGeeks (2)

Output for two sample runs

The above can be achieved with just a single line of code like this :

Python

# import required module

import random

# print random word

print(random.choice(open("myFile.txt","r").readline().split()))

Method 2: Using random.randint()

Steps:

  1. Open the file in read mode using with function
  2. Store all data from the file in a string and split the string into words.
  3. Count the total number of words.
  4. Use random.randint() to generate a random number between 0 and the word_count.
  5. Print the word at that position.

Python

# using randint()

import random

# open file

with open("myFile.txt", "r") as file:

data = file.read()

words = data.split()

# Generating a random number for word position

word_pos = random.randint(0, len(words)-1)

print("Position:", word_pos)

print("Word at position:", words[word_pos])

Output:

Pulling a random word or string from a line in a text file in Python - GeeksforGeeks (3)

Output for two sample runs



Last Updated : 11 Dec, 2020

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

Pulling a random word or string from a line in a text file in Python - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Terence Hammes MD

Last Updated:

Views: 5971

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Terence Hammes MD

Birthday: 1992-04-11

Address: Suite 408 9446 Mercy Mews, West Roxie, CT 04904

Phone: +50312511349175

Job: Product Consulting Liaison

Hobby: Jogging, Motor sports, Nordic skating, Jigsaw puzzles, Bird watching, Nordic skating, Sculpting

Introduction: My name is Terence Hammes MD, I am a inexpensive, energetic, jolly, faithful, cheerful, proud, rich person who loves writing and wants to share my knowledge and understanding with you.