Hi I can't find another question that answers this because it is really basic. I'm learning python and my current code will have to be copy and pasted for every letter of the alphabet. Instead how can I use find()
and ord()
for a range/AZ.
This is my code at the moment:
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)
I want to do this all the way to Z but there must be a way to do that without copying and pasting.
If you can help, thank you so much. I've tried file.find("a","b"...)
but this wouldn't work.
You can use the strings library or just decare the alphabets on your own
with String library:
import string
alphabets = string.ascii_lowercase
print(alphabets)
Outputs:
abcdefghijklmnopqrstuvwxyz
And now you can loop over it:
for characters in alphabets:
print(characters) # replace this with your code
Put your code instead of the print statement. Similarly for uppercase import uppercase from string instead of lowercase.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.