0

Is there a built-in function to give the value of a letter?

what I mean is
a=1
b=2
c=3
d=4
e=5
f=6
...

something like letval(a) would give me 1

4
Contributors
7
Replies
9
Views
7 Years
Discussion Span
Last Post by Kruptein
0

You mean something like this ...

def letval(x):
    print(x)

a = 77
letval(a)

# you can just use
print(a)

Edited by vegaseat: n/a

0

No I don't mean that, that would be stupid.
every letter of the alphabet represents a number
a = the first letter = the first number = 1
b = the second letter = the second number = 2
...
so if I did:

letval(a) #=1
letval(g) #=7 because g is the seventh letter in the alphabet

Edited by Kruptein: n/a

0

hm okay, but in that case I have to make a difference between caps and lowercase letters,... but I will work with that.

0

Convert them all to lower case before using ord:

l =  l.lower()

Edited by scru: n/a

0

If you are only interested in the letter's position in the alphabet, you can use something like this ...

import string

def letter_position(letter):
    ucase = string.uppercase
    pos = ucase.find(letter.upper()) + 1
    if pos:
        print( "%s has position %d in the alphabet" % (letter, pos) )

letter_position('E')
letter_position('f')
letter_position('a')
letter_position('Z')
letter_position('8')  # a number gives no response

Edited by vegaseat: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.