I'm learning python 3 and I'm having problems with string related excerises. One exericse I'm working on is to write a program that cycle through a string and dispaly it 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



     # This my code so far: 
def main():
    first_Name = "s u p e r n a t u r a l"
    for i in range(0,len(first_Name), 2):
        print(first_Name[i:], first_Name)
main()

When I code my code the output is this:

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

Is another for loop needed?

Dani AI

Generated

Quick, practical note for : the goal is to rotate the sequence of letters so each line starts with the next letter and the rest wraps around. Treating the input as a sequence of tokens (letters) is clearer than stepping through the raw spaced string with index arithmetic.

A simple, readable approach using list slicing:

s = "s u p e r n a t u r a l"
letters = s.strip().split()        # ['s','u','p','e',...]
for shift in range(len(letters)):
    print(' '.join(letters[shift:] + letters[:shift]))

If you prefer a mutating approach that avoids rebuilding lists on every loop, use collections.deque and its rotate method:

from collections import deque

letters = deque(s.strip().split())
for _ in range(len(letters)):
    print(' '.join(letters))
    letters.rotate(-1)   # move left by 1 each loop

Notes and troubleshooting:

  • If your original input is "supernatural" (no spaces), use list(word) to get characters, or display spaced characters with ' '.join(word).
  • Remove accidental leading/trailing spaces with strip() before split(); a trailing space creates an empty token.
  • Be careful not to pass two values to print() (e.g., print(a, b)), which prints both separated by a space — that explains why the original output showed the full string alongside the slice.
  • As showed, concatenating two slices is a valid fix; the token and deque variants above keep the logic explicit and easier to reason about.

No, not at all. Here's the modfied code that fixes this:

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

main()

Basicly, you forgot about that range loops though all the numbers upto BUT NOT INCLUDING the upper limit. You see that I've accounted for this by adding 2. Next, since we want the second word to crawl in letter by letter, we need to slice off the start of the second word that can fit in the space left behind by the first word as it's removed bit by bit. Your comma also adds a space, and since we need that gone as well, I've inclued it with the word.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.