3

I'm trying to grab the letters in a string and replace them with an empty space, in this case the empty space is represented by "_" with a space between each underscore.

This is my code:

word = "my example"
def convert_letter(word):
    for i in range(0, len(word)):
        if ord(word[i]) != 32:
            word.replace(word[i], '_')
    print(word)
convert_letter(word)

When I run the code it just returns the word or string and I have no idea why.

Just in case you're wondering the purpose of the if statement is so that it doesn't convert the spaces to "_".

So in the case of this function I'm expecting the following:

_ _  _ _ _ _ _ _ _
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
Sebastian
  • 57
  • 1
  • 2
  • 8
  • 1
    Use `re.replace`, it's easier – Leb Oct 06 '15 at 01:07
  • Can you [edit](http://stackoverflow.com/posts/32960141/edit) your question and add a sample output example? – Hayley Guillou Oct 06 '15 at 01:14
  • I have read through your comments and I think you should specify your problem. You said it was for a Hangman game. Essentially you want as many underscores as there are letters. For that it would for example not be necessary to replace anything at all :) – enpenax Oct 06 '15 at 01:21

3 Answers3

2

The problem in your code is that str.replace doesn't modify a string in place, but returns a copy with the replacement done. You have to assign it back if you want to replace the original string

word = word.replace(word[i], '_')

Make sure you understand the pieces you are working with, if you start with a simple program you can see that replace doesn't perform as you expect

w = 'abc'
w.replace('a', 'x') # should be w = w.replace(...)
print(w) # prints 'abc'

If I take the title of your question literally, you want to replace the letters with underscores only. That would leave all of the special characters and numbers intact. In that case you can use string.ascii_lettrs and join

def convert_letter(word):
    ''.join('_' if c in string.ascii_letters else c for c in word)

As you seem to have stated in other comments, you actually want the underscores to have a space between them, if that is the case then use ' '.join instead

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
0

Try

new_word = word.replace(word[i], '_ ')

From the documentations of string.replace(): It returns a copy of string s with all occurrences of substring old replaced by new.

word = "my example"

def convert_letter(word):
    new_word = word
    for i in range(0, len(word)):
        if ord(word[i]) != 32:
            new_word = new_word.replace(word[i], '_ ')
    print(new_word)
convert_letter(word)
Hayley Guillou
  • 3,953
  • 4
  • 24
  • 34
  • It worked, now my only problem is that it returns all the spaces as one so it looks like this: __ ________ instead of having a space between each underscore. – Sebastian Oct 06 '15 at 01:11
  • 1
    What are you wanting your output to be? – Hayley Guillou Oct 06 '15 at 01:12
  • Instead of having it all together I want a space between each "_" so it would look like this: _ _ _ _ _ _ _ _ _. It's for a game of hangman so it has to be clear how many characters the word has. Instead I'm getting every underscore together which makes each word look like a big, empty line. – Sebastian Oct 06 '15 at 01:17
  • It's almost there. Now It does have spaces between each "_" but for some reason it's not converting every single letter to underscore. So it gives me the following result: _ _ _ _ _ _ pl _. – Sebastian Oct 06 '15 at 01:25
  • @Sebastian that should fix it – Hayley Guillou Oct 06 '15 at 01:29
  • Hey it does work. Nice thank you very much. Just one more question. How is '_' different from '_ '? – Sebastian Oct 06 '15 at 01:31
  • @Sebastian I'm not exactly sure what you're asking. For single vs double quotes see [here](http://stackoverflow.com/questions/56011/single-quotes-vs-double-quotes-in-python). The other single quote that looks like an accent (not sure what it's called) is for inline code like `this`. – Hayley Guillou Oct 06 '15 at 01:37
  • Oh I forgot this page erases extra white spaces. What I meant is why is my result different if I don't put spaces between the single quotation marks and the underscore from the result if I put spaces between the underscore and the single quotation marks? – Sebastian Oct 06 '15 at 01:50
0

If you want to go the regex route:

import re

word = "my example"
word = re.sub('\w', '_ ', word)

Depending on what specifically you want to replace, you may need to alter the regex. \w includes numbers, for instance, so you might want [A-Za-z] instead.

Note that like str.replace(), it also doesn't modify the original.

spenibus
  • 4,339
  • 11
  • 26
  • 35
S. Ellis
  • 11
  • 2