944,158 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2089
  • Python RSS
Oct 18th, 2009
0

letters to numbers

Expand Post »
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
Similar Threads
Reputation Points: 25
Solved Threads: 11
Posting Whiz in Training
Kruptein is offline Offline
258 posts
since Sep 2009
Oct 18th, 2009
0
Re: letters to numbers
You mean something like this ...
python Syntax (Toggle Plain Text)
  1. def letval(x):
  2. print(x)
  3.  
  4. a = 77
  5. letval(a)
  6.  
  7. # you can just use
  8. print(a)
Last edited by vegaseat; Oct 18th, 2009 at 3:18 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 18th, 2009
0
Re: letters to numbers
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:
python Syntax (Toggle Plain Text)
  1. letval(a) #=1
  2. letval(g) #=7 because g is the seventh letter in the alphabet
Last edited by Kruptein; Oct 18th, 2009 at 3:27 pm.
Reputation Points: 25
Solved Threads: 11
Posting Whiz in Training
Kruptein is offline Offline
258 posts
since Sep 2009
Oct 18th, 2009
0
Re: letters to numbers
hint: use ord("a") .
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is online now Online
2,656 posts
since Jul 2008
Oct 18th, 2009
0
Re: letters to numbers
hm okay, but in that case I have to make a difference between caps and lowercase letters,... but I will work with that.
Reputation Points: 25
Solved Threads: 11
Posting Whiz in Training
Kruptein is offline Offline
258 posts
since Sep 2009
Oct 18th, 2009
0
Re: letters to numbers
Convert them all to lower case before using ord:
Python Syntax (Toggle Plain Text)
  1. l = l.lower()
Last edited by scru; Oct 18th, 2009 at 5:07 pm.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Oct 18th, 2009
0
Re: letters to numbers
If you are only interested in the letter's position in the alphabet, you can use something like this ...
python Syntax (Toggle Plain Text)
  1. import string
  2.  
  3. def letter_position(letter):
  4. ucase = string.uppercase
  5. pos = ucase.find(letter.upper()) + 1
  6. if pos:
  7. print( "%s has position %d in the alphabet" % (letter, pos) )
  8.  
  9. letter_position('E')
  10. letter_position('f')
  11. letter_position('a')
  12. letter_position('Z')
  13. letter_position('8') # a number gives no response
Last edited by vegaseat; Oct 18th, 2009 at 5:22 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 19th, 2009
0
Re: letters to numbers
That's the one I needed! thanks
Reputation Points: 25
Solved Threads: 11
Posting Whiz in Training
Kruptein is offline Offline
258 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: List help...
Next Thread in Python Forum Timeline: Multiple Replies Please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC