By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,673 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,673 IT Pros & Developers. It's quick & easy.

How do I replace all characters in a string of unknown length with an underscore

P: 2
I am writing a function that randomly generates a word. I then want to create a new string, call it gameWord, that replaces all the characters in the randomly generated word with _
Nov 4 '13 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 2.5K+
P: 3,551

Without writing the code for you, you would start by finding the length of the word and then create a string of the same length, in which each character is the underscore.

Please show what you may have tried already.

Nov 4 '13 #2

P: 2
I think I got it:

Expand|Select|Wrap|Line Numbers
  1. def gameWord(word):
  2.     """
  3.     swaps out the random word with _, to use while the player is guessing
  4.     """
  5.     underscoreWord=word
  6.     i=0
  7.     for i in range (0,len(word)):
  8.         underscoreWord = underscoreWord.replace(word[i]," _ ")
  9.         i=i+1
  10.     #print word
  11.     return underscoreWord
Nov 6 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.