Don Dix
Don Dix

Reputation: 3

How do I print a table of the alphabet in python?

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.

Upvotes: 0

Views: 2651

Answers (4)

ganja
ganja

Reputation: 31

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])

Upvotes: 0

Mark Tolonen
Mark Tolonen

Reputation: 177600

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.

Upvotes: 0

Arcturus B
Arcturus B

Reputation: 5621

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).

Upvotes: 2

Alon
Alon

Reputation: 112

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.

Upvotes: 1

Related Questions