0

I am writing a program that is supposed to print:

A abcdefghijklmnopqrstuvwxyz
B bcdefghijklmnopqrstuvwxyz
C cdefghijklmnopqrstuvwxyz
D defghijklmnopqrstuvwxyz
E efghijklmnopqrstuvwxyz
F fghijklmnopqrstuvwxyz
G ghijklmnopqrstuvwxyz
H hijklmnopqrstuvwxyz
I ijklmnopqrstuvwxyz
J jklmnopqrstuvwxyz
K klmnopqrstuvwxyz
L lmnopqrstuvwxyz
M mnopqrstuvwxyz
N nopqrstuvwxyz
O opqrstuvwxyz
P pqrstuvwxyz
Q qrstuvwxyz
R rstuvwxyz
S stuvwxyz
T tuvwxyz
U uvwxyz
V wxyz
X xyz
Y yz
Z z

I have written the following code for the program but it does not print out what I want it to. This is what I have written for the program:

alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for k in range(len(alphabet)):
    for j in range(len(alphabet)):
        print(alphabet[j-k],end='')
    print('\n')`

and it prints out:

abcdefghijklmnopqrstuvwxyz

zabcdefghijklmnopqrstuvwxy

yzabcdefghijklmnopqrstuvwx

xyzabcdefghijklmnopqrstuvw

wxyzabcdefghijklmnopqrstuv

vwxyzabcdefghijklmnopqrstu

uvwxyzabcdefghijklmnopqrst

tuvwxyzabcdefghijklmnopqrs

stuvwxyzabcdefghijklmnopqr

rstuvwxyzabcdefghijklmnopq

qrstuvwxyzabcdefghijklmnop

pqrstuvwxyzabcdefghijklmno

opqrstuvwxyzabcdefghijklmn

nopqrstuvwxyzabcdefghijklm

mnopqrstuvwxyzabcdefghijkl

lmnopqrstuvwxyzabcdefghijk

klmnopqrstuvwxyzabcdefghij

jklmnopqrstuvwxyzabcdefghi

ijklmnopqrstuvwxyzabcdefgh

hijklmnopqrstuvwxyzabcdefg

ghijklmnopqrstuvwxyzabcdef

fghijklmnopqrstuvwxyzabcde

efghijklmnopqrstuvwxyzabcd

defghijklmnopqrstuvwxyzabc

cdefghijklmnopqrstuvwxyzab

bcdefghijklmnopqrstuvwxyza

abcdefghijklmnopqrstuvwxyz

I need help to figure out what I did wrong and what I need to do for the code to print what I want it to print.

Matheus Avellar
  • 1,507
  • 1
  • 22
  • 29
Don Dix
  • 3
  • 3
  • 1
    Note that, unlike in the example, each of your printed rows has length 26. Why do you suppose that is? Also, what do you think, e.g., alphabet[-5] is? – Chas Brown Feb 26 '17 at 22:00

4 Answers4

2

There’s a cleaner and shorter solution to your problem:

alphabet = "abcdefghijklmnopqrstuvwxyz"
for i, letter in enumerate(alphabet):
    print(letter.upper(), alphabet[i:])

I suggest you read about Python slices (from the doc, or here on Stack).

Community
  • 1
  • 1
Arcturus B
  • 5,181
  • 4
  • 31
  • 51
1
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for k in range(len(alphabet)):
        for j in range(len(alphabet) - k):
            print(alphabet[j+k],end='')
        print('\n')

I hope it will help you.

Alon
  • 112
  • 1
  • 7
0

Here's a more "pythonic" way that takes advantage of the new f-strings in Python 3.6 as well:

alphabet = 'abcdefghijklmnopqrstuvwxyz'
for i,k in enumerate(alphabet):
        print(f'{k.upper()} {alphabet[i:]}')
  • You can iterate over the characters of a string.
  • enumerate gives both an index the value of the current iteration.
  • f-strings allow expressions to be evaluated in a string.
  • .upper() gives the upper case version of a string.
  • [i:] slice notation returns a sub-string that starts at index i until the end of the string.
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
0

This can be accomplished in two lines of code use the python slice tool and also capitalize at the index:

a = 'abcdefghijklmnopqrstuvqxyz'

add = ''
for i in range(26):
    print(a[i].capitalize(), a[-26:i+1])
ganja
  • 21
  • 3