简体   繁体   English

如何在python中打印字母表?

[英]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.我需要帮助来弄清楚我做错了什么以及我需要做什么才能让代码打印出我想要它打印的内容。

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 ).我建议您阅读有关 Python 切片的信息(来自 doc此处的 Stack )。

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.我希望它会帮助你。

Here's a more "pythonic" way that takes advantage of the new f-strings in Python 3.6 as well:这是一种更“pythonic”的方式,它也利用了 Python 3.6 中的新 f 字符串:

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. enumerate给出了当前迭代的值的索引。
  • f-strings allow expressions to be evaluated in a string. f-strings 允许在字符串中计算表达式。
  • .upper() gives the upper case version of a string. .upper()给出字符串的大写版本。
  • [i:] slice notation returns a sub-string that starts at index i until the end of the string. [i:]切片表示法返回从索引 i 开始直到字符串结尾的子字符串。

This can be accomplished in two lines of code use the python slice tool and also capitalize at the index:这可以在两行代码中使用 python slice 工具完成,并在索引处大写:

a = 'abcdefghijklmnopqrstuvqxyz'

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM