Replace blanks with letter

Fábio Santos fabiosantosart at gmail.com
Tue Aug 20 05:16:55 EDT 2013


On 20 Aug 2013 09:42, <eschneider92 at comcast.net> wrote:
>
> I'm trying to replace the blank(_) with the letter typed in by the user,
in the appropriate blank(_) spot where the letter should be (where is in
the letters list).
>
> letters='abcdefg'
> blanks='_ '*len(letters)
> print('type letter from a to g')
> print(blanks)
> input1=input()
> for i in range(len(letters)):
>     if letters[i] in input1:
>         blanks = blanks[:i] + letters[i] + blanks[i+1:]
>
>
> What am I doing wrong in this code?
>
> Thanks
> Eric

First, don't use range(len(iterable)). It's bad practise, and you will have
to use iterable[i] all the time. Try

for i, letter in enumerate(letters):

If you are modifying a string in-place, you could change it into a list,
then back.

blankslst = list(blanks)
...
blankslst[i] = letters[i]  # or 'letter' if you used enumerate()
...
blanks = ''.join(blankslst)

Now, why are you not printing the `blanks` string again?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130820/f06d7cb5/attachment.html>


More information about the Python-list mailing list