我怎样才能file.find(“ a”,“ b”,“ c” .....等。“ z”)和ord(“ A”,“ B”,“ C” .....等python中的“ Z”)

时间:2020-10-26 19:26:44

标签: python find

嗨,我找不到其他答案可以回答这个问题,因为它确实很基础。我正在学习python,必须将当前代码复制并粘贴到字母表中的每个字母上。相反,我如何使用find()ord()来确定范围/ 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"...),但这是行不通的。

1 个答案:

答案 0 :(得分:0)

您可以使用字符串库,也可以自行取消使用字母

带有字符串库:

import string
alphabets = string.ascii_lowercase
print(alphabets)

输出:

abcdefghijklmnopqrstuvwxyz

现在您可以遍历它:

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

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

相关问题