Dumb noob q: ASCII value of a character

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Horsley

    Dumb noob q: ASCII value of a character

    How can I get the ASCII value of a character, e.g. I have:

    str = "A B"
    for index in range(0, len(str)):
    value = WHAT_GOES_HERE? (str[index])
    print value


    and I hope to get:
    65
    32
    66


    I know the characters will be printable ASCII.
    But how do I get those ASCII character values from the characters???
    I can't seem to find it in the library reference

    Thanks,
    Steve
  • Peter Hansen

    #2
    Re: Dumb noob q: ASCII value of a character

    Steve Horsley wrote:[color=blue]
    >
    > How can I get the ASCII value of a character, e.g. I have:
    >
    > str = "A B"
    > for index in range(0, len(str)):
    > value = WHAT_GOES_HERE? (str[index])
    > print value
    >
    > and I hope to get:
    > 65
    > 32
    > 66
    >
    > I know the characters will be printable ASCII.
    > But how do I get those ASCII character values from the characters???
    > I can't seem to find it in the library reference[/color]

    It's a good idea to read through all the builtin functions, as
    documented in http://www.python.org/doc/current/li...built-in-funcs
    and keep them in mind.

    In this case, ord() (and it's inverse chr()) are what you want.

    -Peter

    Comment

    • vincent wehren

      #3
      Re: Dumb noob q: ASCII value of a character


      "Steve Horsley" <shoot@the.moon > schrieb im Newsbeitrag
      news:c1ldn6$if2 $1@news.freedom 2surf.net...[color=blue]
      > How can I get the ASCII value of a character, e.g. I have:
      >
      > str = "A B"
      > for index in range(0, len(str)):
      > value = WHAT_GOES_HERE? (str[index])
      > print value
      >
      >
      > and I hope to get:
      > 65
      > 32
      > 66[/color]

      Hi Steve,

      a word of advice: do not use the name of an existing function as identifier
      for
      a variable ( I am referring to your usage of str as the name of the
      variable - you are reassigning it!!!). But back to your question:
      What you're looking for is:

      s = "A B"
      for c in s:
      print ord(c)

      HTH,

      Vincent Wehren






      know the characters will be printable ASCII.[color=blue]
      > But how do I get those ASCII character values from the characters???
      > I can't seem to find it in the library reference
      >
      > Thanks,
      > Steve[/color]


      Comment

      • John Hazen

        #4
        Re: Dumb noob q: ASCII value of a character

        * Steve Horsley <shoot@the.moon > [2004-02-26 11:28]:[color=blue]
        > How can I get the ASCII value of a character, e.g. I have:
        >
        > str = "A B"
        > for index in range(0, len(str)):
        > value = WHAT_GOES_HERE? (str[index])
        > print value
        >
        >
        > and I hope to get:
        > 65
        > 32
        > 66[/color]

        ord.

        (And yes, it's a little hard to find. The first time I had to
        solve this problem, I used:
        [color=blue][color=green][color=darkred]
        >>> import string
        >>> string.index(st ring._idmap,'A' )[/color][/color][/color]
        65

        )

        Also of note: Strings can be used as iterators, so there's no need to
        use that indexing trick:
        [color=blue][color=green][color=darkred]
        >>> str = "A B"
        >>> for char in str:[/color][/color][/color]
        .... print ord(char)
        ....
        65
        32
        66[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]


        -John
        < my_first_name AT my_last_name DOT net >

        Comment

        • Steve Horsley

          #5
          Re: Dumb noob q: ASCII value of a character

          vincent wehren wrote:[color=blue]
          > What you're looking for is:
          >
          > s = "A B"
          > for c in s:
          > print ord(c)
          >[/color]
          Thank you everyone who answered.

          Also, "for c in s:" - hmmm , still learning fast!
          Also, overriding str funcion with a local variable - and STILL learning fast!

          Regards,
          Steve

          Comment

          Working...