Random Text Word Fragment Selector

  • Share this:

Code introduction


This function randomly selects a word from the given text and returns a fragment of the word with a length within a specified range.


Technology Stack : Python, random, string

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
def random_word_length(text, min_length, max_length):
    import random
    import string

    words = text.split()
    random_word = random.choice(words)
    word_length = random.randint(min_length, max_length)
    return random_word[:word_length]