letters to numbers

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 98
Reputation: Kruptein is an unknown quantity at this point 
Solved Threads: 5
Kruptein's Avatar
Kruptein Kruptein is offline Offline
Junior Poster in Training

letters to numbers

 
0
  #1
Oct 18th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #2
Oct 18th, 2009
You mean something like this ...
  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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 98
Reputation: Kruptein is an unknown quantity at this point 
Solved Threads: 5
Kruptein's Avatar
Kruptein Kruptein is offline Offline
Junior Poster in Training
 
0
  #3
Oct 18th, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 966
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #4
Oct 18th, 2009
hint: use ord("a") .
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 98
Reputation: Kruptein is an unknown quantity at this point 
Solved Threads: 5
Kruptein's Avatar
Kruptein Kruptein is offline Offline
Junior Poster in Training
 
0
  #5
Oct 18th, 2009
hm okay, but in that case I have to make a difference between caps and lowercase letters,... but I will work with that.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,614
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 131
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso
 
0
  #6
Oct 18th, 2009
Convert them all to lower case before using ord:
  1. l = l.lower()
Last edited by scru; Oct 18th, 2009 at 5:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #7
Oct 18th, 2009
If you are only interested in the letter's position in the alphabet, you can use something like this ...
  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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 98
Reputation: Kruptein is an unknown quantity at this point 
Solved Threads: 5
Kruptein's Avatar
Kruptein Kruptein is offline Offline
Junior Poster in Training
 
0
  #8
Oct 19th, 2009
That's the one I needed! thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC