weird strings question

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

  1. Lucas Raab

    Lucas Raab Guest

    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
    #1
    1. Advertising

  2. Lucas Raab

    John Machin Guest

    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
    #2
    1. Advertising

  3. Lucas Raab

    Robert Kern Guest

    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
    #3
  4. Lucas Raab

    Lucas Raab Guest

    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
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
  2. dorayme
    Replies:
    1
    Views:
    623
    richard
    Jan 21, 2011
  3. richard
    Replies:
    0
    Views:
    587
    richard
    Jan 21, 2011
  4. richard
    Replies:
    0
    Views:
    618
    richard
    Jan 21, 2011
  5. Beauregard T. Shagnasty

    Re: A Weird Appearance for a Weird Site

    Beauregard T. Shagnasty, Jan 21, 2011, in forum: HTML
    Replies:
    1
    Views:
    440
    Captain Paralytic
    Jan 21, 2011
Loading...

Share This Page