0

I'm trying to use a single for loop to cycle over a string to print it out like this:

     s u p e r n a t u r a l
     u p e r n a t u r a l s
     p e r n a t u r a l s u

Here is my code so far:

   def main():
       first_Name = "s u p e r n a t u r a l"
       print(first_Name)
       for i in range(len(first_Name)):
           print(first_Name[i])


main()
3
  • If you need not loop over a long string with many times, how about just duplicate the string like 'HelloHelloHello' and use a single for loop to print it out? Commented Mar 13, 2014 at 3:53
  • possible duplicate of python cyclic shifting of the characters in the string Commented Mar 13, 2014 at 3:56
  • You're actually making it more difficult by preinserting the extra spaces Commented Mar 13, 2014 at 3:59

5 Answers 5

1

I have it in 4 lines:

li = list('supernatural')
for c in li:
    print ''.join(li)
    li.append(li.pop(0))
Sign up to request clarification or add additional context in comments.

Comments

0
In [11]:

S='s u p e r n a t u r a l'
SL=S.split(' ')
for i in range(len(S)-S.count(' ')):
    print ' '.join(SL[i:]+SL[:i])
s u p e r n a t u r a l
u p e r n a t u r a l s
p e r n a t u r a l s u
e r n a t u r a l s u p
r n a t u r a l s u p e
n a t u r a l s u p e r
a t u r a l s u p e r n
t u r a l s u p e r n a
u r a l s u p e r n a t
r a l s u p e r n a t u
a l s u p e r n a t u r
l s u p e r n a t u r a

Comments

0

What a coincidence, I was doing the very same thing earlier in my consulting work! But seriously, to get you started on your homework, here are some ideas:

>>> print first_Name[0:] + ' ' + first_Name[:0]
s u p e r n a t u r a l
>>> print first_Name[1:] + ' ' + first_Name[:1]
 u p e r n a t u r a l s
>>> print first_Name[2:] + ' ' + first_Name[:2]
u p e r n a t u r a l s

Looks promising, at least the even numbers...

How to iterate through just even numbers?

>>> help(range)

step is your friend here.

>>> range(0, len(first_Name), 2)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

I bet you can (and should) figure out the rest.

Comments

0

A simple way of doing this:

#!/usr/bin/env python

def rshift(s, i):
    i = i % len(s)
    return s[-i:] + s[0:-i]

if __name__ == '__main__':
    s = "12345"
    for i in range(len(s)):
        print rshift(s, -i)

Comments

0

To me, this is made for a deque

import collections
d = collections.deque('supernatural')
for _ in range(len(d)):
    print(' '.join(d))
    d.rotate(-1)

Prints out:

s u p e r n a t u r a l
u p e r n a t u r a l s
p e r n a t u r a l s u
e r n a t u r a l s u p
r n a t u r a l s u p e
n a t u r a l s u p e r
a t u r a l s u p e r n
t u r a l s u p e r n a
u r a l s u p e r n a t
r a l s u p e r n a t u
a l s u p e r n a t u r
l s u p e r n a t u r a

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.