我怎么能 file.find("a","b","c"..... 等 "z") 和 ord("A","B","C".......等等 "Z") 在 python

How can I file.find("a","b","c"..... etc. "z") and ord("A","B","C"..... etc. "Z") in python

您好,我找不到另一个问题来回答这个问题,因为它真的很基础。我正在学习 python 并且必须为字母表中的每个字母复制和粘贴我当前的代码。相反,我如何使用 find()ord() 作为 range/A-Z.

这是我目前的代码:

A = (ord("A")-64)*(file.find("a"))
if A < 0:
    A = 0
B = (ord("B")-64)*(file.find("b"))
if B < 0:
    B = 0
C = (ord("C")-64)*(file.find("c"))
if C < 0:
    C = 0

print(A+B+C)

我想一直这样做到 Z,但必须有一种无需复制和粘贴即可做到这一点的方法。

如果能帮上忙,万分感谢。我试过 file.find("a","b"...) 但这行不通。

您可以使用字符串库或自行删除字母表

使用字符串库:

import string
alphabets = string.ascii_lowercase
print(alphabets)

输出:

abcdefghijklmnopqrstuvwxyz

现在你可以遍历它了:

for characters in alphabets:
    print(characters) # replace this with your code

输入您的代码而不是打印语句。同样,对于大写字母,从字符串而不是小写字母导入大写字母。