Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert alphabet letters to number in Python

Tags:

python

How can the following be finished?

characters = ['a''b''c''d''e''f''g''h''i''j''k''l''m''n''o''p''q''r''t''u''v''w''x''y''z'] numbers = ['1''2''3''4''5''6''7''8''9''10''11''12''13''14''15''16''17''18''19''20''21''22''23''24'] text = raw_input(' Write text: ') 

I've tried to solve it many ways, but couldn't get to the pint. I want to make exc. If I type "hello" the output to be in numbers lined like in alphabet. Example a = 1 < in alphabet.

like image 205
altin Avatar asked Dec 25 '10 01:12

altin


People also ask

How do I convert letters to numbers?

The Letter-to-Number Cipher (or Number-to-Letter Cipher or numbered alphabet) consists in replacing each letter by its position in the alphabet , for example A=1, B=2, Z=26, hence its over name A1Z26 .

How do I get the alphabet number in Python?

Use the ord() Function to Convert Letters to Numbers in Python. The ord() function in Python is utilized to return the Unicode , or in this case, the ASCII value of a given letter of the alphabet. We will apply the ord() function to the letters and subtract 96 to get the accurate ASCII value.

Do letters have values in Python?

In Python, each Unicode character, such as “a”, “b”, or “c” is an integer under the hood. For example, here is the Unicode table for the English alphabet. In Python, you can check the integer value of any character using the built-in ord() function.


2 Answers

What about something like this:

print [ord(char) - 96 for char in raw_input('Write Text: ').lower()] 

ord
list comprehension
ASCII character codes

EDIT
Since you asked me to explain I will... though it has been explained pretty well in the comments already by [?].

Let's do this in more that one line to start.

input = raw_input('Write Text: ') input = input.lower() output = [] for character in input:     number = ord(character) - 96     output.append(number) print output 

This does the same thing, but is more readable. Make sure you can understand what is going on here before you try to understand my first answer. Everything here is pretty standard, simple Python. The one thing to note is the ord function. ord stand for ordinal, and pretty much every high level language will have this type of function available. It gives you a mapping to the numerical representation of any character. The inverse function of ord is called chr.

chr(ord('x')) == 'x' # for any character, not just x. 

If you test for yourself, the ordinal of a is 97 (the third link I posted above will show the complete ASCII character set.) Each lower case letter is in the range 97-122 (26 characters.) So, if you just subtract 96 from the ordinal of any lower case letter, you will get its position in the alphabet assuming you take 'a' == 1. So, ordinal of 'b' == 98, 'c' == 99, etc. When you subtract 96, 'b' == 2, 'c' == 3, etc.

The rest of the initial solution I posted is just some Python trickery you can learn called list comprehension. But, I wouldn't focus on that as much as I would focus on learning to solve the problem in any language, where ord is your friend. I hope this helps.

like image 78
sberry Avatar answered Sep 28 '22 00:09

sberry


You can use chr() and ord() to convert betweeen letters and int numbers.

Here is a simple example.

>>> chr(97) 'a' >>> ord('a') 97 
like image 23
Statham Avatar answered Sep 27 '22 23:09

Statham