3

I apologize beforehand if this has already been covered, but I've been unable to find an answer.

I want a for loop to produce an output with every other letter having a space before it. Should look something like this:

>>>everyOther('banana')
 b
a
 n
a
 n
a

I understand the general concept of for loops and so far I get how:

def everyOther(word):
    for each in word:
        print each

produces:

 w
 o
 r
 d

but I'm not sure how to select which letters would have a space before it.

Any help or directions would be very helpful. ^o^

SIRHC
  • 33
  • 3

3 Answers3

1

You can use enumerate() to return an incrementing index number for each letter in the string, along with the letter itself.

You can then check to see if the number is divisible by 2, using the modulus operator (remember, they start at 0). If it's not divisible by 2, print the space first to get your desired output.

def everyOther(word):
    for index, letter in enumerate(word):
        if index % 2 == 0:
            print letter
        else:
            print " " + letter

Here's an example:

In [7]: everyOther("bananas")
 b
a
 n
a
 n
a
 s
Will
  • 24,082
  • 14
  • 97
  • 108
  • /@Will delete your answer, I wrote the pseudo code OP would learn. – Merlin Jun 04 '16 at 22:31
  • Why? I explained it and linked to documentation so OP can learn. I don't want to play games with people; some learn better by example. – Will Jun 04 '16 at 22:32
  • your answer means OP will not learn how to figure out coding. – Merlin Jun 04 '16 at 22:32
  • no, then people come here expecting others to write code for them! What you just did. You dont think I could have written it. – Merlin Jun 04 '16 at 22:33
  • I'm sure you could have. But I'm here to help people. I learn much better when I can see an example before my eyes. And let's face it, it took about 45 seconds to write, test, and explain it. Everyone has different learning styles.If you find my answer to be in violation of the rules, by all means, flag it, but its not. If you find the question to violate the rules, close vote it. While my style is different than yours, there's nothing wrong with it. – Will Jun 04 '16 at 22:38
  • 1
    Thanks for the help. I understanding both you're reasoning. @Merlin As per your suggestions, I did some research on enumerate() and now have a better understanding on how it works. – SIRHC Jun 04 '16 at 23:32
  • 1
    @Will Thanks for your help too. In in the examples and explanations I saw, a list was used. I didn't know how it could also apply to a string. But after seeing an example I get it now. – SIRHC Jun 04 '16 at 23:37
  • Awesome, no problem, very glad to help. I'm the same way, sometimes it's easier to learn by example :) – Will Jun 05 '16 at 02:17
0

I would try something like this. It checks the index of the character, and puts a space in front if it's odd. Otherwise, it just prints like normal.

def everyOther(word):      
    final=""  
    for i in word:
        index=word.index(i)
        if index%2!=0:
            final+=i
        else:
            print i
    print final
Arete
  • 13
  • 2
0
is_odd = True
for each in word:
    if is_odd: print(' ',) #use print(' ', end="") in py 3.x
    print(each)
    is_odd = not is_odd

using word.index(i) iterates all over the list and slows down. So, if there are n letters in word, there are n**2 iterations. but this approach needs only n iterations, and hence faster.

Aravind Voggu
  • 1,491
  • 12
  • 17