Reputation: 1933
I'm working on a simple python game in which the player attempts to guess letters contained in a word. The problem is, when I print a word, it's printing the \n at the end.
From my initial research, I think I need to use r.string() to remove it. However, I'm not sure where it would go.
Sorry for the newbie question.
import random
with open('wordlist.txt') as wordList:
secretWord = random.sample(list(wordList), 1)
print (secretWord)
Upvotes: 4
Views: 21220
Reputation: 97
text = open("your file","r").read().replace('\n','')
x = set(text.split(" "))
Upvotes: 1
Reputation: 439
The other answers are better in my opinion, but you can also use:
secretWord = secretWord.replace('\n', ''))
EDIT: OP stated secretWord is a list, not a string.
In the case of a list of strings, use:
secretWord = [each.replace('\n', '') for each in secretWord]
Upvotes: 9
Reputation: 298196
You can use .strip()
to strip out whitespace:
secret_word = random.choice(wordList).strip()
Upvotes: 8