weird strings question

L

Lucas Raab

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
 
J

John Machin

Lucas said:
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
.... 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.
 
R

Robert Kern

Lucas said:
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
(e-mail address removed)

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

Lucas Raab

Robert said:
Lucas said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top