Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52731

Re: Replace blanks with letter

References <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com>
Date 2013-08-20 10:16 +0100
Subject Re: Replace blanks with letter
From Fábio Santos <fabiosantosart@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.54.1376990224.19984.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On 20 Aug 2013 09:42, <eschneider92@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?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Replace blanks with letter eschneider92@comcast.net - 2013-08-20 01:35 -0700
  Re: Replace blanks with letter Fábio Santos <fabiosantosart@gmail.com> - 2013-08-20 10:16 +0100
  Re: Replace blanks with letter Peter Otten <__peter__@web.de> - 2013-08-20 11:19 +0200
  Re: Replace blanks with letter eschneider92@comcast.net - 2013-08-20 13:45 -0700
    Re: Replace blanks with letter Dave Angel <davea@davea.name> - 2013-08-20 21:54 +0000
  Re: Replace blanks with letter eschneider92@comcast.net - 2013-08-21 00:49 -0700
    Re: Replace blanks with letter Chris Angelico <rosuav@gmail.com> - 2013-08-21 19:24 +1000
    Re: Replace blanks with letter Dave Angel <davea@davea.name> - 2013-08-21 11:42 +0000
    Re: Replace blanks with letter John Gordon <gordon@panix.com> - 2013-08-21 14:35 +0000
  Re: Replace blanks with letter eschneider92@comcast.net - 2013-08-22 15:42 -0700
    Re: Replace blanks with letter Dave Angel <davea@davea.name> - 2013-08-23 01:28 +0000
  Re: Replace blanks with letter eschneider92@comcast.net - 2013-08-22 15:44 -0700

csiph-web