# weird strings question

Discussion in 'Python' started by Lucas Raab, Feb 26, 2005.

1. ### Lucas RaabGuest

Is it possible to assign a string a numerical value?? For example, in
the string "test" can I assign a number to each letter as in "t" = 45,
"e" = 89, "s" = 54, and so on and so forth??

TIA

Lucas Raab, Feb 26, 2005

2. ### John MachinGuest

Lucas Raab wrote:
> Is it possible to assign a string a numerical value?? For example, in

> the string "test" can I assign a number to each letter as in "t" =

45,
> "e" = 89, "s" = 54, and so on and so forth??
>
> TIA

>>> for c in 'abcd':

.... print c, ord(c)
....
a 97
b 98
c 99
d 100

If that isn't what you mean, you may need to rephrase your question.

John Machin, Feb 26, 2005

3. ### Robert KernGuest

Lucas Raab wrote:
> Is it possible to assign a string a numerical value?? For example, in
> the string "test" can I assign a number to each letter as in "t" = 45,
> "e" = 89, "s" = 54, and so on and so forth??

Use a dictionary with the strings as keys.

string2num = {}
string2num['t'] = 45
string2num['e'] = 89

etc.

--
Robert Kern

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Robert Kern, Feb 26, 2005
4. ### Lucas RaabGuest

Robert Kern wrote:
> Lucas Raab wrote:
>
>> Is it possible to assign a string a numerical value?? For example, in
>> the string "test" can I assign a number to each letter as in "t" = 45,
>> "e" = 89, "s" = 54, and so on and so forth??

>
>
> Use a dictionary with the strings as keys.
>
> string2num = {}
> string2num['t'] = 45
> string2num['e'] = 89
>
> etc.
>

Thanks. That's what I was looking for, but was just unsure exactly how
to proceed.

Lucas Raab, Feb 27, 2005