How to remove a random word from a string in Python

1 Answer

0 votes
import random

def remove_random_word(input_str):
    words = input_str.split(" ")
    if not words:
        return input_str

    random_index = random.randint(0, len(words) - 1)
    words.pop(random_index)  # Remove the randomly selected word

    return " ".join(words)


input_str = "I'm not clumsy The floor just hates me"
result = remove_random_word(input_str)

print(result)




'''
run:

I'm not clumsy The floor hates me

'''

 



answered May 5 by avibootz
...