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

## All 7 Replies

You mean something like this ...

``````def letval(x):
print(x)

a = 77
letval(a)

# you can just use
print(a)``````

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``````

hint: use `ord("a")` .

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

Convert them all to lower case before using ord:

``l =  l.lower()``

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``````

That's the one I needed! thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.