Random Replacement of words using Python

Random word replacement is a text manipulation technique where we randomly select a word from input text and replace it with a randomly chosen word from a predefined list. This process introduces variation and generates different text versions, useful for content generation, testing, and creative writing.

Python provides excellent tools for implementing random word replacement through the random module, which helps generate random indices for selecting words from text and replacement lists.

Syntax

The key functions used for random word replacement are ?

random.randint(start, stop)

Returns a random integer from the specified range.

  • start Starting position (inclusive)

  • stop Ending position (inclusive)

str.split(separator, maxsplit)

Splits a string into a list of words.

  • separator Character to split on (default: whitespace)

  • maxsplit Maximum number of splits (default: -1 for all)

str.join(iterable)

Joins elements of an iterable into a single string.

  • iterable Sequence of strings to join

Implementation Example

Here's a complete implementation of random word replacement ?

import random

def random_word_replacement(text, word_list):
    # Split text into individual words
    words = text.split()
    
    # Generate random indices
    random_index_in_text = random.randint(0, len(words) - 1)
    random_index_in_list = random.randint(0, len(word_list) - 1)
    
    # Replace the randomly selected word
    words[random_index_in_text] = word_list[random_index_in_list]
    
    # Join words back into a string
    replaced_text = ' '.join(words)
    return replaced_text

# Example usage
replacement_words = ["Good", "Better", "Best"]
original_text = "Tutorials Point is a good place for learning"
result = random_word_replacement(original_text, replacement_words)

print(f"Original: {original_text}")
print(f"Modified: {result}")
Original: Tutorials Point is a good place for learning
Modified: Tutorials Point is a Better place for learning

How It Works

The algorithm follows these steps ?

  1. Split the input text into individual words using split()

  2. Generate a random index for the text (0 to length of words - 1)

  3. Generate a random index for the replacement word list

  4. Replace the word at the random text position with the word at the random list position

  5. Join the modified word list back into a string using join()

Multiple Replacements Example

You can perform multiple random replacements in a single text ?

import random

def multiple_random_replacements(text, word_list, num_replacements=2):
    words = text.split()
    
    for _ in range(num_replacements):
        if len(words) > 0:  # Check if there are words to replace
            random_index_in_text = random.randint(0, len(words) - 1)
            random_index_in_list = random.randint(0, len(word_list) - 1)
            words[random_index_in_text] = word_list[random_index_in_list]
    
    return ' '.join(words)

# Example with multiple replacements
replacement_words = ["Amazing", "Excellent", "Outstanding"]
original_text = "Python is a great programming language for beginners"
result = multiple_random_replacements(original_text, replacement_words, 2)

print(f"Original: {original_text}")
print(f"Modified: {result}")
Original: Python is a great programming language for beginners
Modified: Python is a Outstanding programming Amazing for beginners

Use Cases

Random word replacement has practical applications in ?

  • Content Generation Creating variations of marketing copy or social media posts

  • Text Augmentation Generating training data for machine learning models

  • Chatbots Adding variety to automated responses

  • Creative Writing Exploring different word choices and styles

  • A/B Testing Testing different versions of text content

Conclusion

Random word replacement is a powerful technique for introducing variation in text content. Using Python's random module with basic string operations, you can easily implement this functionality for content generation, testing, and creative applications.

The technique is particularly valuable in natural language processing, chatbot development, and automated content creation where text diversity is essential for engaging user experiences.

---
Updated on: 2026-03-27T14:47:36+05:30

461 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements