shahaadn
shahaadn

Reputation: 1

How to strip whitespace from element in list

I read over a file, scraped all the artist names from within the file and put it all in a list. Im trying to pull out one artist only from the list, and then removing the space and shuffling all the letters (word scrabble).

artist_names = []    
rand_artist = artist_names[random.randrange(len(artist_names))] #Picks random artist from list]
print(rand_artist)

howevever when i print rand_artist out, sometimes i get an artist with lets say 2 or 3 words such as "A Northern Chorus" or "The Beatles". i would like to remove the whitespace between the words and then shuffle the words.

Upvotes: 0

Views: 50

Answers (1)

user
user

Reputation: 5696

First replace whitespaces with empty strings. Then turn the string to a list of characters. Since i guess you want them to be lowercase, I included that as well.

import random

s = "A Northern Chorus".replace(' ','').lower()

l=list(s)
random.shuffle(l)

print(l)

Also, you can use random.choice(artist_names) instead of randrange().

Upvotes: 1

Related Questions