I am writing a program that is supposed to generates a 26-line block of letters using a loop containing one or two print statements
Question:
I am supposed to have an output with the below pattern with 26-line block as shown below.
abcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyza
cdefghijklmnopqrstuvwxyzab
...
yzabcdefghijklmnopqrstuvwx
zabcdefghijklmnopqrstuvwxy
But I am not getting it right. Please check my code below and help me.
This is my code:
alphabet = "abcdefghijklmnopqrstuvwxyz"
for i in range(26):
print (alphabet[1: 26] + alphabet[0])
The output:
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
Answers:
The Python standard library has a handy list-like data structure called a deque
(pronounced like "deck"). It is a double ended queue that has a useful method, rotate
, that shifts the contents of its elements. It will make short work of your task:
from collections import deque
import string
alphabet = deque(string.ascii_lowercase)
for _ in range(len(alphabet)):
print("".join(alphabet))
alphabet.rotate(-1)
Output:
abcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyza
cdefghijklmnopqrstuvwxyzab
defghijklmnopqrstuvwxyzabc
...
Your problem: you are always printing the same string, namely alphabet[1:26] + alphabet[0]
.
Instead use:
alphabet = "abcdefghijklmnopqrstuvwxyz"
for i in range(26):
print(alphabet[i : 27] + alphabet[0 : i]
Notice how i
keeps getting larger as we run through the string and how this impacts that starting character.
I am supposed to have an output with the below pattern with 26-line block as shown below.
abcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyza
cdefghijklmnopqrstuvwxyzab
...
yzabcdefghijklmnopqrstuvwx
zabcdefghijklmnopqrstuvwxy
But I am not getting it right. Please check my code below and help me.
This is my code:
alphabet = "abcdefghijklmnopqrstuvwxyz"
for i in range(26):
print (alphabet[1: 26] + alphabet[0])
The output:
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
bcdefghijklmnopqrstuvwxyza
The Python standard library has a handy list-like data structure called a deque
(pronounced like "deck"). It is a double ended queue that has a useful method, rotate
, that shifts the contents of its elements. It will make short work of your task:
from collections import deque
import string
alphabet = deque(string.ascii_lowercase)
for _ in range(len(alphabet)):
print("".join(alphabet))
alphabet.rotate(-1)
Output:
abcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyza
cdefghijklmnopqrstuvwxyzab
defghijklmnopqrstuvwxyzabc
...
Your problem: you are always printing the same string, namely alphabet[1:26] + alphabet[0]
.
Instead use:
alphabet = "abcdefghijklmnopqrstuvwxyz"
for i in range(26):
print(alphabet[i : 27] + alphabet[0 : i]
Notice how i
keeps getting larger as we run through the string and how this impacts that starting character.