Search code examples
pythonprintingrandom

Pick a Random Word On a Single Line In Python?


How would I pick a single line and pick all the words on that line and then print them to the screen using a set. This is what I have so far:

import random

test = ['''
line 1
line 2
line 3
line 4
line 5
line 6
''']
print (random.choice(test))

Solution

  • I think this does exactly what you're asking:

    print random.choice(test[0].split('\n'))
    line 5
    

    I have a feeling you're looking to do a little more than that, though.